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
https://forums.phpfreaks.com/topic/16475-checking-form-data/
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
https://forums.phpfreaks.com/topic/16475-checking-form-data/#findComment-68752
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
https://forums.phpfreaks.com/topic/16475-checking-form-data/#findComment-68777
Share on other sites

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.