Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,505
    Thanks
    3
    Thanked 501 Times in 488 Posts
    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/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,102
    Thanks
    23
    Thanked 594 Times in 593 Posts
    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>

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    @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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •