Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-19-2012, 01:50 PM   PM User | #1
Damien001
New to the CF scene

 
Join Date: Aug 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Damien001 is an unknown quantity at this point
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
Damien001 is offline   Reply With Quote
Old 08-19-2012, 05:35 PM   PM User | #2
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,379
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
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>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 08-19-2012, 06:04 PM   PM User | #3
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,495
Thanks: 18
Thanked 361 Times in 360 Posts
sunfighter is on a distinguished road
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>
sunfighter is offline   Reply With Quote
Old 08-19-2012, 06:13 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 08-19-2012, 06:20 PM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
@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
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Tags
java, number, range

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:18 PM.


Advertisement
Log in to turn off these ads.