#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    95
    Rep Power
    12

    Question Regex help


    I wish to validate a form field so the content is in a specific format.

    The format I wish it to be in is firstname underscore surname underscore number.

    e.g. john_smith_012345

    Can anybody help with the correct RegEx to meet this formatting.

    Thanks in advance
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    This has nothing to do with JavaScript, so you rather should use the Regex forum.

    It's also not clear what the names will look like. Does everything have to be lowercase like your example suggests? Do you allow special characters like in "Raśl"? What about middle names and things like "Jr."?

    You know, not every name is as simple as "John Smith".
  4. #3
  5. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,131
    Rep Power
    4304
    *** Thread moved ***
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  6. #4
  7. /*
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    729
    Rep Power
    620
    Here is a simple method:

    regex Code:
    (\w*)_(\w*)_(\d*)
    */
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    29
    Rep Power
    0
    benno32, it should be + instead of * if you don't want to allow an empty first name / surname:

    regex Code:
    (\w+)_(\w+)_(\d+)

    Comments on this post

    • benno32 agrees : Good point.

IMN logo majestic logo threadwatch logo seochat tools logo