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 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Noblesville, IN
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    validate that field can ONLY contains numbers and a decimal point

    how do i validate a text field so that it can only contain numbers (0-9) and an optional decimal point?
    Ed Ventura
    Graphic/Web Designer
    www.blackwatercompany.com

  • #2
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    I got this mostly from irt.org. It just checks to see if math can be done on the input, which should be any valid number that a user could enter.

    Code:
    <html>
    <script language="javascript">
    function check(contents) {
        if (((contents / contents) != 1) && (contents != 0)) 
    	alert('This box requires a number')
        else
    	alert('This is a number')
    }
    </script>
     
    
    <body>
    <form name="myform">
    <input type="text" name="textbox1">
    <input type="button" onclick="check(document.myform.textbox1.value)" value="verify">
    </form>
    </body>
    </html>

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function isDecimal( str )
    {
    return /^\d*\.?\d*$/.test( str );
    }
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What beetle said. Although if I'm not mistaken that would also return true for an empty or null string?

    An easy fix?:

    <script type="text/javascript">
    <!--
    function isDecimal( str )
    {
    return str != "" ? /^\d*\.?\d*$/.test( str ) : false;
    }
    // -->
    </script>
    Last edited by whammy; 01-15-2003 at 02:26 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could do it that way, or build it into the regex

    function isDecimal( str )
    {
    return /^(?:\d+)|(?:\d*\.?\d*)$/.test( str );
    }

    C'mon Whammy, aren't you always lording over regexs?
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah... but I was just fixing it quick... BTW the regex above doesn't work, it seems to accept everything, even "asdf". And I'm not sure about the non-matching(?) i.e. (?:whatever) groups, if you're only testing against the regex - isn't that unnecessary if you aren't going to be replacing per group, i.e. '$1 $2', and you don't want the group to be "assigned" to $1 or whatever?

    As you can see, I forgot the technical terms, lol.

    I fixed it regex only like this:

    Code:
    <script type="text/javascript"> 
    <!-- 
    function isDecimal(str) {
       return /^\d+(\.\d+)?$|^\.\d+$/.test(str);
    }
    // --> 
    </script>
    I'm sure beetle can shorten it though, and I am now eagerly awaiting his response.

    ... is there an "and/or" operand for regex's?

    Just because my signature says 'learn em, doesn't mean I've TOTALLY mastered them yet! But I can definitely write regex's that get the job done, I just make sure I test the (*&^ out of them.
    Last edited by whammy; 01-16-2003 at 02:40 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  

    Posting Permissions

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