CobraSW Posted August 19, 2012 Share Posted August 19, 2012 (edited) 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 August 19, 2012 by CobraSW Link to comment Share on other sites More sharing options...
niche Posted August 19, 2012 Share Posted August 19, 2012 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 comment Share on other sites More sharing options...
dsonesuk Posted August 19, 2012 Share Posted August 19, 2012 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 comment Share on other sites More sharing options...
justsomeguy Posted August 20, 2012 Share Posted August 20, 2012 This might be a better pattern to use: var anum=/^(\D+)(\.\D+)?$/ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now