SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot GWild's Avatar
    Join Date
    May 2005
    Location
    Colorado
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Assistance with Javascript Regular Expression

    Hello.

    I have a javascript form validation routine which uses a regular expression to validate the input of a text box.

    I need a regular expression which will require (anywhere in the input) at least 1 number, and the entire contents can also contain zero or more commas, apostrophes, and spaces.

    The regular expression I've constructed is very close but does not quite work:

    ^[0-9, *]*[0-9][0-9, *]$

    It will not accept, for example, input of a single digit, but it will accept something such as "2*".

    How can I modify this to allow digits, commas, spaces, apostrophes, but also require at least 1 number?

    Any help is appreciated.
    Lang ist der weg und hart,
    das aus der hoelle fuehrt auf zum licht.


    My Site

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    The info about optional expressions will help here, where you use a question mark to indicate optional parts of the expression.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot GWild's Avatar
    Join Date
    May 2005
    Location
    Colorado
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    The info about optional expressions will help here, where you use a question mark to indicate optional parts of the expression.
    I'll review that material.


    Thank you for the response.
    Lang ist der weg und hart,
    das aus der hoelle fuehrt auf zum licht.


    My Site

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GWild View Post
    ^[0-9, *]*[0-9][0-9, *]$
    The * inside the [] includes it as an acceptable character, just put it after.

    /^[\d\s\'\,]*\d[\d\s\'\,]*$/

    For me it's less ambiguous to use \s for spaces. The same for comma and apostrophe which don't actually need to be escaped.
    Tab-indentation is a crime against humanity.

  5. #5
    SitePoint Zealot GWild's Avatar
    Join Date
    May 2005
    Location
    Colorado
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    The * inside the [] includes it as an acceptable character, just put it after.

    /^[\d\s\'\,]*\d[\d\s\'\,]*$/

    For me it's less ambiguous to use \s for spaces. The same for comma and apostrophe which don't actually need to be escaped.
    That appears to have remedied the issue. Thanks.

    I will, of course, make use of the information at the link provided by PMW57.
    Lang ist der weg und hart,
    das aus der hoelle fuehrt auf zum licht.


    My Site


Bookmarks

Posting Permissions

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