harchew Posted February 22, 2008 Share Posted February 22, 2008 How do I validate an user name in such a way that, it must contain at least an alpha and number. Only alpha,number and ._ are allow for the user name otherwise return invalid. Link to comment https://forums.phpfreaks.com/topic/92380-validate-username/ Share on other sites More sharing options...
uniflare Posted February 22, 2008 Share Posted February 22, 2008 you would use regular expresions, i believe there is another forum specifically for these questions: http://www.phpfreaks.com/forums/index.php/board,43.0.html ---- im not so good with regex in php but you could do: <?php $string = "USER._NAME23"; // ex valid username if(preg_match("/[^a-zA-Z0-9\._]/i",$string) == 0){ // valid characters if(preg_match("/[0-9]/i",$string) == 0){ // No Numbers Found echo("Username must contain at least 1 number"); } if(preg_match("/[a-z]/i",$string) == 0){ // No Letters Found echo("Username must contain at least 1 character"); } $strlen = strlen($string); if($strlen < 8 && $strlen > 16){ echo("Username must be between 8 and 16 characters in length"); } }else{ echo("You have illegal characters in your username, Legal characters include: A-Z, 0-9, a period (.), and an underscore ( _ )"); } ?> hope this helps, Link to comment https://forums.phpfreaks.com/topic/92380-validate-username/#findComment-473336 Share on other sites More sharing options...
harchew Posted February 22, 2008 Author Share Posted February 22, 2008 Its working perfectly. Thank you so much (Bow...) Link to comment https://forums.phpfreaks.com/topic/92380-validate-username/#findComment-473391 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.