Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2005
    Location
    Ontario, Canada
    Posts
    1,750

    Need help with Regular Expression

    I'm writing some JavaScript code right now to validate user input. I want to test for the following:

    * string must be 5 to 24 characters long
    * first character can only be: a-z, A-Z
    * last character can only be: a-z, A-Z, 0-9
    * all other characters can only be: a-z, A-Z, 0-9, period(.), dash(-), underscore(_)

    Could someone show me how to build a regular expression to test a string for all these conditions.
    C++, 3D OpenGL and Game Programming video tutorials:
    www.MarekKnows.com
    Play my free games: Ghost Toast, Zing, Jewel Thief

  2. #2
    Join Date
    Mar 2006
    Location
    South Australia
    Posts
    4,521
    Stand back, I know regular expressions!

    Code:
    ([a-zA-Z])([a-zA-Z0-9.-_]){3,22}([A-Za-z])
    I'm pretty sure the parentheses can be omitted. Note: I have not tested this, and I am still half asleep.

  3. #3
    Join Date
    Mar 2005
    Location
    Ontario, Canada
    Posts
    1,750
    Hmmm, that doesn't seem to be working quite right.

    When I type "3aaaaa" it says it passes. I want to prevent digits as the first character.

    Here is a website that can be used to test expressions: http://www.w3schools.com/js/tryit.as...js_regexp_test
    C++, 3D OpenGL and Game Programming video tutorials:
    www.MarekKnows.com
    Play my free games: Ghost Toast, Zing, Jewel Thief

  4. #4
    Join Date
    Mar 2006
    Location
    South Australia
    Posts
    4,521
    Sorry, I forgot the start line, end line characters. This one should work:

    Code:
    ^[a-zA-Z]([a-zA-Z0-9.-_]){3,22}[A-Za-z0-9]$
    breakdown:

    ^ start matching at the start of the line
    [a-zA-Z] match a single letter (upper or lower case)
    ([a-zA-Z0-9.-_]){3,22} match between 3 and 22 characters that are either numeric, letters, or the . - or _ symbols
    [A-Za-z0-9] match a single alphanumeric character
    $ end of line
    Last edited by mr_charisma; 05-20-2010 at 08:32 PM. Reason: didn't see the 0-9 for the last character

  5. #5
    Join Date
    Mar 2005
    Location
    Ontario, Canada
    Posts
    1,750
    Sorry, that doesn't work either. It fixed the problem with the case of "3aaaaa" but this new expression is letting this be valid "aa>aa"

    I do not want to allow the greater than symbol in the string.

    is it interpretting the dash as "allow all characters between the period and the underscore"?

    I tried ^[a-zA-Z]([a-zA-Z0-9.\-_]){3,22}[A-Za-z0-9]$ but that didn't work either

    wait a minute, the period is special too. I just tried ^[a-zA-Z]([a-zA-Z0-9\.\-_]){3,22}[A-Za-z0-9]$ but that doesn't work either
    Last edited by mmakrzem; 05-20-2010 at 08:37 PM.
    C++, 3D OpenGL and Game Programming video tutorials:
    www.MarekKnows.com
    Play my free games: Ghost Toast, Zing, Jewel Thief

  6. #6
    Join Date
    Mar 2006
    Location
    South Australia
    Posts
    4,521

  7. #7
    Join Date
    Apr 2004
    Location
    Nr London, UK
    Posts
    831
    dot matches any character.

    /^[a-z][a-z0-9\.\-_]{3,23}[a-z0-9]$/i

    no need for parentheses since you didn't say anything about capturing, i modifier should make it case insensitive thus not requiring a-zA-Z.

  8. #8
    Join Date
    Mar 2005
    Location
    Ontario, Canada
    Posts
    1,750
    Thank you! That seems to work now!
    C++, 3D OpenGL and Game Programming video tutorials:
    www.MarekKnows.com
    Play my free games: Ghost Toast, Zing, Jewel Thief

Tags for this Thread

Posting Permissions

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