how do i validate a username/password using ereg/eregi?

requirement for username is
- min. 3 character, max. 12 character
- first character range from a-z
- any characters in the middle range from a-z, 0-9, -, _
- last character range from a-z, 0-9

requirement for password
- min, 3 chars, max 12 chars
- all characters range from a-z, 0-9

username example
abc123 - valid
-abc123 - invalid
_abc123 - invalid
abc123- - invalid
123abc - invalid
a123_cb2d - valid

Is there any good reference for ereg?
I found the example in the php help file is not complete.
Thanks!

    never mind, didn't finish reading the post. sorry, the guy below has the right idea

      Errrr - forgive me if this is wrong - I dont have time to test it, but try....

      preg_match("/[a-z][a-z0-9-_]{1,10}[a-z0-9]$/i", $strUsername);

      for the username

      and

      preg_match("/[a-z0-9-_]{3,12}$/i", $strPassword);

      for the password

        well actually it was between 3 and 12 letters long. You can actually do regular expression checking with javascript, which would be useful here.
        Its always best to have PHP check it as well, just incase the user has javascript switched-off.

        you want a reg expression something like:

        if (ereg("^[a-z][a-z0-9\-_]{1,10}[a-z0-9]$", $var))
        {
            username OK
        }
        else
        {
           username not OK
        }
        

        Try here for a decent run down.

        Javascript regular expressions match in a similar way, (the execution is slightly different though)

        This is a slight modification on a reg - ex i currently use, i think it should work

        function check_date(input)
        {
        	var date_format = /^[a-z][a-z0\-9-_]{1,10}[a-z0-9]$/ ;
        	var matcharray = date_format.exec(input.value) ;
        	if (!matcharray)
        	{
                return 0;
        	}
        	else
        	{
        		return 1;
        	}
        }
        

        Run the function as part of a validation script, it takes the input name as a variable, and outputs 0 if it doesn't match and 1 if it does.

        Jamie

          Thanks guy!
          My problem solved, you guys are great! 🙂
          and the tutorial site is helpful..

            Write a Reply...