Ugluth Posted December 22, 2010 Share Posted December 22, 2010 Hello I just read the tutorial on the site about regular expresions which was really nice. After reading i tried putting some of that knowledge in practice with no results though. I only want the user to enter letters and not digits. So I used if (isset($_POST['name']) && preg_match("/^[A-Za-z$]/", $_POST['name'])) { $name = mysql_real_escape_string($_POST['name']); echo "ok"; } else { echo "Category name must be a word!<br />"; When i enter something starting with a letter it passes the check. If something starts with a number it doesnt. The problem is that if i enter asdf123 it passes the test, while it shouldn't. I think it has something to do with the $ but i'm not really sure whats wrong with it. Could someone please tell me what I'm doing wrong? I'm sure its something pretty silly but i can't figure it out. Link to comment https://forums.phpfreaks.com/topic/222405-check-that-only-characters-are-in-a-form/ Share on other sites More sharing options...
Ugluth Posted December 22, 2010 Author Share Posted December 22, 2010 Terribly sorry for the post but i just figured it out right after posting and checking the tutorial again, sorry for the trouble. preg_match("/^[A-Za-z]+$/", $_POST['name']) Fixed my problem. If there's something wrong with using this please do tell me. Link to comment https://forums.phpfreaks.com/topic/222405-check-that-only-characters-are-in-a-form/#findComment-1150405 Share on other sites More sharing options...
Pikachu2000 Posted December 23, 2010 Share Posted December 23, 2010 Using a pattern for that is overkill. ctype_alpha would be fine, presuming spaces are also disallowed. Link to comment https://forums.phpfreaks.com/topic/222405-check-that-only-characters-are-in-a-form/#findComment-1150609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.