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. Quote Link to comment 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; } Quote Link to comment 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" Quote Link to comment 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 Quote Link to comment 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 } Quote Link to comment 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"; } Quote Link to comment 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)) Quote Link to comment Share on other sites More sharing options...
squiblo Posted January 19, 2010 Author Share Posted January 19, 2010 Thank you everyone! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.