SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Location
    MA, USA
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need Feedback: Working on a regex to validate input

    I'm working on a regex to validate a form field's input. I've just begun to learn about regular expressions and would like to see if anyone has any feedback.

    What I'm trying to do:
    • The input can contain multiple words
    • Can not contain any symbols other than white space and - (dash)
    • I don't want the first character to be a number
    • After the first character the string can be alpha-numeric



    Valid Examples:
    • My Name
    • SomeName1
    • Another-Name Folks



    Invalid Examples:
    • 1or The Other
    • *Hello Dr. *Jones
    • What_is_Going on



    Here's what I came up with:
    ^[A-z]+[A-z0-9\s-]+[A-z0-9]

    It seems to be working fine, but I'm not sure if there are any shortcuts I may be able to use, or if I'm leaving anything out.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Location
    MA, USA
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, after some more testing I've come up with this process:

    First I strip any white space from the begin and the end of the string using this regex: ^\s+|\s+$

    Then I perform a validation check using this regex: ^[A-z]+[A-z0-9\s-]+[A-z0-9]$


Tags for this Thread

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
  •