Lodius2000 Posted May 16, 2008 Share Posted May 16, 2008 is there a php function to validate a textbox entry that will only allow a combination of letters and/or numbers, I am trying to set up a username submission form and I want only letters and/or numbers in the username, no spaces, no punctuation, no nothing, if there isnt a function what could be my way of invalidating special characters and spaces and all that, I would like it to roughly match my current sytax for error validation which is. //check that username is less or equal to than 14 characters if (strlen(trim($_POST['username'])) <= 14) { $errors[]= "Your username must be 14 characters or less."; } thanks for your help Link to comment https://forums.phpfreaks.com/topic/105854-allowing-only-letters-and-numbers/ Share on other sites More sharing options...
p2grace Posted May 16, 2008 Share Posted May 16, 2008 Awesome command: <?php $clean = ctype_alnum($string); // returns true if only letters and numbers returns false otherwise. ?> http://www.php.net/manual/en/function.ctype-alnum.php Link to comment https://forums.phpfreaks.com/topic/105854-allowing-only-letters-and-numbers/#findComment-542559 Share on other sites More sharing options...
sasa Posted May 16, 2008 Share Posted May 16, 2008 if (preg_match('/[^A-z0-9]/',trim($_POST['username']))) echo 'wrong'; else echo 'OK'; Link to comment https://forums.phpfreaks.com/topic/105854-allowing-only-letters-and-numbers/#findComment-542560 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.