Jump to content

problem with checking a value within in a form is a number in a set range


Recommended Posts

Trying to write a script that will check the value in the form is a number between the range of 1 to 99999. 1) The script work mostly except for two things if you put a number then letter it won’t detect that you entered letters as well as numbers 2) I want it to run the script on onblur event, however every time I try the script does not work at all. Did study java years and year ago and found I forgotten most things if people can point out where I am going wrong would be very greatful

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type" /><title>Please enter your age</title><script language="JavaScript1.2">function checknumber(){var x=document.checknum.pnum.value/*does it contain non digits */var anum=/(^\D+$)|(^\D+\.\D+$)/if (anum.test(x)){alert("Please input a valid Runner ID between 1 to 99999 !")testresult=false}/*is it above 99999*/else if (x > 99999){alert("Please input a valid Runner ID between 1 to 99999 !")testresult=false}/*is it below 1*/else if (x < 1){alert("Please input a valid Runner ID between 1 to 99999 !")testresult=false}return (testresult)}</script></head><body><form name="checknum" onSubmit="return checknumber()">Please input a valid Runner ID between 1 to 99999:<input type="text" name="pnum" onchange="checknumber()"><input type="submit" value="Submit"></form></body></html>

Many Thanks Damien

Edited by CobraSW
Link to post
Share on other sites

What's your console telling you? Also, you can put an alert() at the top of your function and start your debugging from their. Also, it's important to know java has no connection to javascript as a coding language.

Link to post
Share on other sites

Use getElementById, and id ref to target input element, if single alert message apply it at end if validation is false, if multiple validation messages required, concatenate each message and again show at end.

function checknumber(){/*var x=document.checknum.pnum.value*/var testresult = true;var x=document.getElementById("pnum");if(isNaN(x.value)){testresult=false;}/*does it contain non digits */var anum=/(^\D+$)|(^\D+\.\D+$)/if (anum.test(x.value)){testresult=false;}/*is it above 99999*/else if (x.value > 99999){testresult=false;}/*is it below 1*/else if (x.value < 1){testresult=false;}if(!testresult){alert("Please input a valid Runner ID between 1 to 99999 !")}return (testresult)}

<form name="checknum" onSubmit="return checknumber()">Please input a valid Runner ID between 1 to 99999:<input type="text" id="pnum" name="pnum" onchange="checknumber()"><input type="submit" value="Submit"></form>

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...