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

php string validation

Last post 10-24-2007, 10:51 PM by Aussie Susan. 1 replies.
Sort Posts: Previous Next
  •  10-24-2007, 10:21 PM 35954

    php string validation

    Hi.  regex hurts my brain.  This simple function will validate a username.

    1. between 4 and 20 characters

    2. can only be numbers or letters
     

    function f_validate_username($username) {
        $normal = "^([a-zA-Z0-9]{4,20})$";
        if (eregi($normal, $username)) {
              return true;
        }
           else {
              return false;
        }
    }

     

    What I want to add, but can't figure out... is how to check to make sure it doesn't START with a number.

     

    matches:  ab1cde , aBcd1, aaaa, a1111

    doesn't match:   1acbd

     

    Thanks :) 

  •  10-24-2007, 10:51 PM 35955 in reply to 35954

    Re: php string validation

    Try:

    ^([a-zA-Z][a-zA-Z0-9]{3,19})$

    Remember, the quantifier only applies to the immediately preceding item.

    Susan

View as RSS news feed in XML