CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   problem with checking a value within in a form is a number in a set range (http://www.codingforums.com/showthread.php?t=270869)

Damien001 08-19-2012 01:50 PM

problem with checking a value within in a form is a number in a set range
 
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

Code:

<!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

vwphillips 08-19-2012 05:35 PM

Code:

<!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
 if (!isFinite(x)||x<0||x>99999){
  alert("Please input a valid Runner ID between 1 to 99999 !")
  return false;
 }
 return x;
}
</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>


sunfighter 08-19-2012 06:04 PM

javascript has changed a lot over the years. Maybe this site might help you http://www.quirksmode.org/js/contents.html or look at http://www.w3schools.com/js/js_examples.asp

For a slightly different way for doing your problem (and I spent some time on it so want some one to look at it LOL) Here is my take:

Code:

<!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 type="text/javascript">
function checknumber()
{
        var num = document.getElementById('pnum').value;
        if(isNaN(num) == true){
                document.getElementById('title').style.color = "red";
                document.getElementById('title').innerHTML = "You must enter a valid number between 1 to 99999";
                document.getElementById('pnum').value = "";
        }else if((num > 99999) || (num < 1)){
                document.getElementById('title').style.color = "blue";
                document.getElementById('title').innerHTML = "number not between between 1 to 99999";
        }else if((num <= 99999) || (num >= 1)){
                document.getElementById('title').style.color = "black";
                document.getElementById('title').innerHTML = "Correct";

        }
}
function setFocus()
{
document.getElementById("pnum").focus();
}
</script>
</head>
<body onload="setFocus()">
<div id="title">Please input a valid number between 1 to 99999</div>
<input type="text" id="pnum" name="pnum" onblur="checknumber();" />
</body>
</html>


AndrewGSW 08-19-2012 06:13 PM

I haven't tested it but

else if((num <= 99999) || (num >= 1)) {

should be

else if((num <= 99999) && (num >= 1)) {

although I prefer to write it

Code:

else if((num >= 1) && (num <= 99999)) {
but it seems okay other than this.

AndrewGSW 08-19-2012 06:20 PM

@Damien001
If you see code like this

Code:

<script language="JavaScript1.2">
then it is very old and should be avoided. It should be:

Code:

<script type="text/javascript">
And single statements in JS are completed with a semi-colon; This does not apply to compound statements such as if () { } (although an exception is the do-while statement).

Valuable document: Code Conventions


All times are GMT +1. The time now is 10:12 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.