Jump to content

Check that only characters are in a form


Ugluth

Recommended Posts

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.