note 43492 deleted from function.preg-match by nlopess
| From: | nlopess@php.net | Date: | Wed, 23 Jun 2004 09:16:08 +0000 |
| Subject: | note 43492 deleted from function.preg-match by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71539@lists.php.net to get a copy of this message | ||
Note Submitter: unleaded@unleadedonline.net
----
Here's a function you can use to check a username, the constraints are as per the comment.
<?php
/**
* Checks a username
"The length of the username is greater than or equal to 6, less than or
equal to 16, and the username only contains characters in the
range A-Z, a-z and 0-9. The username has at least 1 alpha and one numeric character."
Examples:
short - BAD - too short
veryveryveryverylong - BAD - too long
TooWordy - BAD - no non-alphanumerics
12345678 - BAD - no letters
!2Wordy - BAD - non-alphanum's present
*/
// Checks a username, returns false if to short or not valid. Returns true is a nice username.
function check_username($username)
{
switch (false)
{
// check to see it is all alphanumeric and between 6 - 16 chars long, inclusive
case preg_match("/^[A-Za-z0-9]{6,16}$/", $username);
// Check to see there is at least one alpha
case preg_match("/[A-Za-z]{1,}/", $username);
// Check to see if there is at least one number
case preg_match("/[0-9]{1,}/", $username);
return false;
break;
default:
return true;
}
} // check username
?>