note 43492 added to function.preg-match

From: Date: Wed, 23 Jun 2004 06:36:26 +0000
Subject: note 43492 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-71530@lists.php.net to get a copy of this message
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 ?> ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43492 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43492&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43492&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#71530) next »