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

Thread: Form validation

  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Form validation

    Hi, I am fairly new to JavaScript. and I am trying to find a script to validate a field for a survey. More specifically check if a field only has numbers. I found this script on the net, but there is one part of it that I don’t understand…
    Code:
    function checknumber() {
        var x=document.checknum.pnum.value
        var anum=/(^\d+$)|(^\d+\.\d+$)/
        if (anum.test(x))
            testresult=true
        else{
            alert("Please input a valid number!")
            testresult=false
        }
        return (testresult)
    }
    </script>
    <script>
    function checkban() {
        if (document.layers||document.all||document.getElementById)
            return checknumber()
        else
            return true
    }
    </script>
    What I don’t understand is what this specific part is doing well more how it works… I was wondering if someone could shead some light on my little problem here.. thanks
    Code:
    var anum=/(^\d+$)|(^\d+\.\d+$)/

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Loic View Post
    Code:
    var anum=/(^\d+$)|(^\d+\.\d+$)/
    This is a regular expression with the pattern to be tested contained between the / characters.

    (^\d+$) means one or more digits and nothing but (e.g. 12345 but not A12345)

    | or

    (^\d+\.\d+$) means one or more digits followed by a decimal point (\.) followed by one or more digits, and nothing but. (e.g. 123.45 but not A123.45).

    In short, nothing but a number with or without decimal places is allowed.


    You can test your regular expressions at: http://www.ogauge.co.uk/regextester.html


  •  

    Posting Permissions

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