squiblo Posted January 19, 2010 Share Posted January 19, 2010 If a user enters data into an input field, i do not want them to be able to upload special characreters like the following or numbers: "- #'][{}£$%^&*()â▀♣ and many more from character map. how can i do this, by checking a variable like... $username = $_POST['username']; Thanks. Link to comment https://forums.phpfreaks.com/topic/189080-special-characters-and-symbol-problem/ Share on other sites More sharing options...
mapleleaf Posted January 19, 2010 Share Posted January 19, 2010 Can you manage letters only?? function alpha_only($string) { return (preg_match("/[A-Z\s_]/i", $string) > 0) ? true : false; } Link to comment https://forums.phpfreaks.com/topic/189080-special-characters-and-symbol-problem/#findComment-998290 Share on other sites More sharing options...
squiblo Posted January 19, 2010 Author Share Posted January 19, 2010 what exactly will that function do, because i need an else saying something like, "You can only use letters" Link to comment https://forums.phpfreaks.com/topic/189080-special-characters-and-symbol-problem/#findComment-998292 Share on other sites More sharing options...
mapleleaf Posted January 19, 2010 Share Posted January 19, 2010 exactly only allows letters. If it is usernames you are doing you could use this php function: ctype_alnum ( string $text ) Check the maual http://php.net/manual/en/function.ctype-alnum.php Link to comment https://forums.phpfreaks.com/topic/189080-special-characters-and-symbol-problem/#findComment-998296 Share on other sites More sharing options...
Psycho Posted January 19, 2010 Share Posted January 19, 2010 Can you manage letters only?? function alpha_only($string) { return (preg_match("/[A-Z\s_]/i", $string) > 0) ? true : false; } That function will not work. It simply returns true if ANY ONE character matches the pattern! The pattern needs to be exclusionary (Besides, why would you test if something is true, just to return true or false? Just return the result of the test. See example below) function validCharacters($input) { //Returns true if all the characters are letters (upper and lower case) return (preg_match("/[^\w]/", $input)==0); } //Usage if (!validCharacters($_POST['username'])) { echo "Username can only contain letters"; } else { //Input was valid } Link to comment https://forums.phpfreaks.com/topic/189080-special-characters-and-symbol-problem/#findComment-998297 Share on other sites More sharing options...
squiblo Posted January 19, 2010 Author Share Posted January 19, 2010 The following function only works for the variable "$forname" how can i get it to also work for $surname and $city function validCharacters($input) { //Returns true if all the characters are letters (upper and lower case) return (preg_match("/[^\w]/", $input)==0); } //Usage if (!validCharacters($forname)+($surname)+($city)) { echo "All boxes can only contain letters"; } else { echo "success"; } Link to comment https://forums.phpfreaks.com/topic/189080-special-characters-and-symbol-problem/#findComment-998305 Share on other sites More sharing options...
Psycho Posted January 19, 2010 Share Posted January 19, 2010 Typically you would validate each field individually and give the user specific details about what fields need to be changed, but if you want to do it all in one go, then use the correct method for concatenating string - which is a period: if (!validCharacters($forname.$surname.$city)) Link to comment https://forums.phpfreaks.com/topic/189080-special-characters-and-symbol-problem/#findComment-998315 Share on other sites More sharing options...
squiblo Posted January 19, 2010 Author Share Posted January 19, 2010 Thank you everyone! Link to comment https://forums.phpfreaks.com/topic/189080-special-characters-and-symbol-problem/#findComment-998316 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.