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 8 of 8

Thread: number check

  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Posts
    219
    Thanks
    25
    Thanked 1 Time in 1 Post

    number check

    NEVER MIND, problem is solved

    Hi there, I want to check if a field is number only.. I've tried with the code below..though when i fill in something like '33q' it doesn't display the alert..
    what am i doing wrong ?

    <html>


    <script language="JavaScript" type="text/javascript" defer="defer">

    function checkhuis(eleID){
    eleObj = document.forms[0].elements[eleID];
    regex = /^[0-9]/;

    if ( eleObj.value.length !=0 ){
    if ( regex.test(eleObj.value) == false) {
    alert('please adjust your number');
    eleObj.focus();
    }

    }
    }

    </script>


    <form name="formpje">
    Enter a number then click the button:
    <input type=text name="input" id="test" size=10 value="" onblur="checkhuis(this.id)">



    <br><br>


    </form>
    </html>
    Last edited by docock; 02-26-2007 at 12:47 PM.

  • #2
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Try parsing it to int - parseInt and then check if the value is NaN.

  • #3
    Regular Coder
    Join Date
    Feb 2007
    Posts
    219
    Thanks
    25
    Thanked 1 Time in 1 Post
    ive tried that without any succes :S
    Isn't there a way to use a normal regexp ?

  • #4
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Code:
    function checkhuis()
    {
    var obj = document.forms[0].input;
    if(obj.value == "NaN")
    alert("Not a number!");
    }

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by BarrMan View Post
    Code:
    function checkhuis()
    {
    var obj = document.forms[0].input;
    if(obj.value == "NaN")
    alert("Not a number!");
    }
    The NaN value cannot be tested for with the == equality operator.
    Correct syntax is:

    if (isNaN (obj.value)) {

  • #6
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Oh, sorry, my mistake.

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    I like this hack:
    Code:
    <script type="text/javascript">
    <!--
    function checkhuis(n) {
    if ((n != 0)&&(n/n != 1)) alert('Numbers Only');
    }
    // -->
    </script>
    
    <input type=text name="input" id="test" size=10 value="" onblur="checkhuis(this.value)">
    Last edited by rwedge; 02-27-2007 at 12:00 AM.

  • #8
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    if(isNaN(n)){alert('numbers only');return false;}
    else{return true;}
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.


  •  

    Posting Permissions

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