Jump to content

Checking form data


mastermike707

Recommended Posts

[code]$title = (isset($_POST['title'])) ? strip_tags(htmlentities($_POST['title'])) : false;
$err = ($title && preg_match("/[A-Za-z0-9]{3, 50}/", $title)) ? '' : "Your title must be at least three characters long and only letters and numbers.<br />\n";
$author = (isset($_POST['author'])) ? strip_tags(htmlentities($_POST['author'])) : false;
$err .= ($author && preg_match("/[A-Za-z0-9]{3, 50}/", $author)) ? '' : "Your name must be at least three characters long and only letters and numbers.<br />\n";
$email = (isset($_POST['email'])) ? strip_tags(htmlentities($_POST['email'])) : false;
$err .= ($email && preg_match("/[+]@[+]\.[A-Za-z]{2,3}/", $email)) ? '' :"Your e-mail address must be valid.<br />\n";
$comment = (isset($_POST['comment'])) ? strip_tags(htmlentities($_POST['comment']))  : false;
$err .= ($comment && strlen($comment) > 3 && strlen($comment) < 500) ? '' :"You must have at least 3 characters in your comment, but not over 500.<br />\n";[/code]
This code never works correctly, even when the values are correct. Any idea why?
Link to comment
Share on other sites

Then either your register_globals is turned off or you're preg_match pattern is incorrect.

I checked your pattern, and your error is really...well, stupid.

You put :

[code]/[A-Za-z0-9]{3, 50}/[/code]

Correct one would be :

[code]/[A-Za-z0-9]{3,50}/[/code]

Notice the lack of space between "3," and "50"? Yup, the space was your error :)
Link to comment
Share on other sites

You're using :

/[+]@[+]\.[A-Za-z]{2,3}/

To match any character, it's prefer to use (.+?) instead of [+]. You'll also need to put a \ in front of the @ to avoid any warnings, and you'll probably want to change the {2,3} to {2,5}, since some are obviously longer. Also, what about .qc.ca, or any subdomain? Eh?

You should use :

/(.+?)\@(.+?)\.([a-zA-Z0-9]\.[a-zA-Z0-9]{2,5}|[a-zA-Z0-9]{2,5})/

I'm no expert in REGEX though, so it's not optimized.
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.