Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Validate username, pasword and text in HTML and PHP.

Last post 05-17-2009, 6:33 AM by sempsteen. 8 replies.
Sort Posts: Previous Next
  •  05-15-2009, 11:14 PM 53071

    Validate username, pasword and text in HTML and PHP.

    Hi, I'm very new to regular expressions and need some help. I'm using HTML and PHP (with MySQL).

    I have a couple of validation requirements I was wondering if someone could me in.

    Validation #1

    Validation rules:
     + be 5 to 8 characters in length
     + only contain number(s) or letter(s)
     + must start with a letter
     + must contain at least one letter and one number

    Examples that will match:
      David4
      JO34B

    Examples that will Not match:
      4Peter
      jac1
      fred_9

    My failed attempt: ^(?=.{5-8})([[:alpha:]])+([0-9]{1})*




    Validation #2

    Validation rules:
     + be 4-10 characters in length
     + only contain number(s) or letter(s)
     + must contain at least one number and at least two letters

    Examples that will match:
      victorzz77
      JO34B

    Examples that will Not match:
      j361
      fred_9



    Validation #3

    Validation rules:
     + be 3-50 characters in length
     + only contain letter(s), hyphens(-) or space(s)

    Examples that will match:
      The quick brown fox
      James-Brown

    Examples that will Not match:
      9 feet below
      2a

    My failed attempt: ^(?=.{3-50})(?=.*[a-zA-Z\q- \e])

  •  05-16-2009, 4:04 AM 53072 in reply to 53071

    Re: Validate username, pasword and text in HTML and PHP.

    Untested suggestions with some short explanations:

    #1
    ^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z][a-zA-Z\d]{4,7}$
    ^                       // start of the string
    (?=.*[a-zA-Z])          // must "see" 1 letter when looking ahead
    (?=.*\d)                // must "see" 1 digit when looking ahead
    [a-zA-Z]                // match 1 letter
    [a-zA-Z\d]{4,7}         // match 4 to 7 letters or digits
    $                       // end of the string


    #2
    ^(?=(.*[a-zA-Z]){2})(?=.*\d)[a-zA-Z\d]{4,10}$
    ^                       // start of the string
    (?=(.*[a-zA-Z]){2})     // must "see" 2 letters when looking ahead
    (?=.*\d)                // must "see" 1 digit when looking ahead
    [a-zA-Z\d]{4,10}        // match 4 to 10 letters or digits
    $                       // end of the string


    #3
    ^[a-zA-Z\s-]{3,50}$
    ^                       // start of the string
    [a-zA-Z\s-]{3,50}       // match 3 to 50 letters. white space characters or hyphens
    $                       // end of the string


    Note that inside a character class, the hyphen does not need to be escaped if placed at the end, or start of the character class:

    [a-c]   // matches 'a', 'b' or 'c'
    [ac-]   // matches 'a', 'c' or '-'
    [-ac]   // matches '-', 'a' or 'c'
    [a\-c]  // matches 'a', '-' or 'c'
  •  05-16-2009, 4:09 AM 53073 in reply to 53072

    Re: Validate username, pasword and text in HTML and PHP.

    B.t.w., I forgot to compliment you with the way you asked your question: a detailed, and clearly communicated question with enough examples of what should, and shouldn't be matched. If only all questione here looked like that!

  •  05-16-2009, 8:28 AM 53074 in reply to 53071

    Re: Validate username, pasword and text in HTML and PHP.

    Key point for the first two validations: Think lookaheads like regex inside validators.

    First One:
    ^(?=[a-zA-Z]+\d)[a-zA-Z0-9]{5,8}$
    * Ensure that the variable contains a number which comes after at least one letter.

    Second One:
    ^(?=[a-zA-Z]*\d)(?=(\d*[a-zA-Z]){2})[a-zA-Z0-9]{4,10}$
    * Ensure that the variable contains a number which may have letter(s) before it.
    * Ensure that the variable contains two letters (either side by side or separated by a number).

    Third One:
    ^[-a-zA-Z ]{3,50}$

  •  05-16-2009, 1:12 PM 53079 in reply to 53074

    Re: Validate username, pasword and text in HTML and PHP.

    sempsteen:
    ...

    Second One:
    ^(?=[a-zA-Z]*\d)(?=([a-zA-Z]\d*){2})[a-zA-Z0-9]{4,10}$
    ...

    If the string starts with a number (the OP did not mention it should start with a letter!), your regex will fail.

  •  05-16-2009, 3:12 PM 53081 in reply to 53079

    Re: Validate username, pasword and text in HTML and PHP.

    Good point. I think this should do the job:

    ^(?=[a-zA-Z]*\d)(?=(\d*[a-zA-Z]){2})[a-zA-Z0-9]{4,10}$

    Thanks

  •  05-17-2009, 3:43 AM 53092 in reply to 53081

    Re: Validate username, pasword and text in HTML and PHP.

    sempsteen:

    Good point. I think this should do the job:

    ^(?=[a-zA-Z]*\d)(?=(\d*[a-zA-Z]){2})[a-zA-Z0-9]{4,10}$

    Thanks

    Yes!

    ; )

  •  05-17-2009, 4:23 AM 53093 in reply to 53092

    Re: Validate username, pasword and text in HTML and PHP.

    Fantastic, thank you so much!

     

     

  •  05-17-2009, 6:33 AM 53096 in reply to 53093

    Re: Validate username, pasword and text in HTML and PHP.

    np
View as RSS news feed in XML