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.

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.