SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Regular Expression

    Hi, I am looking for a regular expression rule to validate string that can have alphabet, astrophe and forward slash or only aplhabet, no others character is allowed.
    I have tried using /^[a-zA-Z*\/']/ but it does allow invalid character like &^$ as well. Hope somebody can guide me. Thanks

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    var str = "abcABC!,/1";
                if(str.match('[^a-zA-Z,/!]+')) {
                    alert('Error');
                }

    the above will throw an error if anything in the str variable is not valid.


  3. #3
    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)
    That one seems to have a comma instead of an apostrophe.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks gRoberts. But, I have 2 questions to ask,
    1) [^a-zA-Z,/!]+ -> what is the function of the plus sign at behind?
    2) if I would like to add on the white space to be allowed, why i can't put [^a-zA-Z,/!\s] or [^a-zA-Z,/!(\s)]?

    Thanks, pmw57 too. Yeah, I have removed the comma and replaced with astrophe.

  5. #5
    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 plus sign means to match one or more characters

    You can use in [^a-zA-Z'/! ] with the space explicitly included.
    It may be possible that negating the whitespace character causes problems, such that it's matching non-whitespace characters (normal characters) and throwing up an error.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks pmw57. It doesn't work as expected, i have tried this validation before it will throw me syntax error.

  7. #7
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Regex:
    [^a-zA-Z\'`/! ]+

    When using single quotations to specify the regex, you have to escape any single quotations that are within it by prefixing it with a backward slash.


  8. #8
    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)
    Curses, I missed that. I was using the forward slash for my own tests.

    When using quotes, my editor (Notepad++) shows syntax colouring troubles when the single quote isn't escaped, which would have helped to nail down the trouble.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol it's the small things that catch us out mate.


  10. #10
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both of you. Learn much from both of you. But, for regular expression, I still have a long way to go to master it. Thanks again

  11. #11
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a number of websites that are invaluable when it comes to regex..

    http://regexlib.com/
    http://www.regular-expressions.info/

    The javascript section at regular-expressions.info is particularity useful in your situation.

    http://www.regular-expressions.info/javascript.html



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
  •