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 to the CF scene
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Regular expression for natural numbers

    I'm trying to write an expression to validate a natural number. It should accept any non-negative integer including '0' but not '01'. It seems like the simplest regular expression imaginable, but I can't get it to work correctly in javascript. The problem is that it will except negative numbers, and will in fact accept 'x10'.

    Code:
    function isNaturalNumber (str) {
    	var pattern = /^(0)|([1-9][0-9]*)$/;
    	return pattern.test(str);
    }
    It seems to honor the '$' but not the '^'. I know there are other ways to accomplish this, but I'd really like to understand why this doesn't work, since I need to validate some more complicated data.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Parentheses.
    also, [0-9] = \d

    var pattern = /^(0|([1-9]\d*))$/;

    Code:
    function isNaturalNumber (str) {
    	var pattern = /^(0|([1-9]\d*))$/;
    	return pattern.test(str);
    }

  • Users who have thanked mrhoo for this post:

    rickyd (09-12-2008)

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    As long as you're actually not using the submatches
    Code:
    var pattern = /^(?:0|[1-9]\d*)$/;
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • Users who have thanked liorean for this post:

    rickyd (09-12-2008)

  • #4
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks both for your help. I understand that \d == [0-9], but I find the latter much easier to read. I can see why both suggestions are improvements, but I still don't understand why my original code didn't work. Can you explain?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,988
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    var pattern = /^(0)|([1-9][0-9]*)$/;

    specifies that the regex will return true if either:

    a) The first character is the digit 0 regardless of what follows (so 01 or 0x will return true)

    OR

    b) the pattern includes (but not necessarily starts with) a digit from 1-9 followed by zero or more digits from 0-9 at the end of the string.
    (so x22 will return true but 22x will return false)

    As mrhoo says, it is a matter of parentheses.


    You can test your regular expressions at: http://www.claughton.clara.net/regextester.html

  • Users who have thanked Philip M for this post:

    rickyd (09-12-2008)

  • #6
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Got it. Thanks so much.


  •  

    Posting Permissions

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