Jump to content

Archived

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

jo.nova

PHP form validation

Recommended Posts



[b]I'm trying to figure out how to validate form fields using PHP.  My form already checks for empty fields, but I would also like to validate the email format and the minimum phone number length. 

Any suggestions? Or links to some good scripts for this purpose?
[/b]

Share this post


Link to post
Share on other sites
In my opinion [url=http://php.net/preg_match]preg_match[/url] is better.

Share this post


Link to post
Share on other sites
[code]$regex = "^[A-Za-z0-9\._-]+@([A-Za-z0-9][A-Za-z0-9-]{1,62})(\.[A-Za-z][A-Za-z0-9-]{1,62})+$";[/code]
Make that a variable that can be accessed by the script
then pass this into the script
[code]if (!ereg("$regex", $_POST['email'])) { //tests emails format with regex variable above
$errorhandler .= "The email address is improperly formatted<br />";
}[/code]
The $_POST['email']
will be the name of your variable.
The errorhandler is what I use to record the errors but handle them however you want the important part is
if (!ereg( and the rest of it until )) {
That is the most important if the email is not formatted like
something@something.com
then it returns false/

Share this post


Link to post
Share on other sites
[quote author=Daniel0 link=topic=100058.msg394452#msg394452 date=1152542894]
In my opinion [url=http://php.net/preg_match]preg_match[/url] is better.
[/quote]
Hmm what are a major advantages over each?

Share this post


Link to post
Share on other sites
[quote author=designationlocutus link=topic=100058.msg394458#msg394458 date=1152543082]
[quote author=Daniel0 link=topic=100058.msg394452#msg394452 date=1152542894]
In my opinion [url=http://php.net/preg_match]preg_match[/url] is better.
[/quote]
Hmm what are a major advantages over each?
[/quote]

I find preg_match faster and ereg is not binary-safe.

Share this post


Link to post
Share on other sites
[quote author=Daniel0 link=topic=100058.msg394463#msg394463 date=1152543403]
[quote author=designationlocutus link=topic=100058.msg394458#msg394458 date=1152543082]
[quote author=Daniel0 link=topic=100058.msg394452#msg394452 date=1152542894]
In my opinion [url=http://php.net/preg_match]preg_match[/url] is better.
[/quote]
Hmm what are a major advantages over each?
[/quote]

I find preg_match faster and ereg is not binary-safe.
[/quote]

Hmm might have to give both functions a whizz and give them some difficult data to play with :) Thanks for the answer.

Share this post


Link to post
Share on other sites
Preg match or ereg can be used interchangeably, it just has different syntax rules, one is based on Perl regular expressions hte other is based on post ix(?) I think it is
One is better than the other, pregmatch statistically is better and has more performance.
But ereg is good for small things, like checking emails, or whatever, and I think pregmatch has slightly harder to understand syntax, but it's better overall, atleast by what I have studied

Share this post


Link to post
Share on other sites
Hmm so for the process of validating small form data use ereg() and preg_match() for larger dataset. You know it might be a good convention to use ereg() with varchar fields and preg_match () for the larger blobs and text fields.

Share this post


Link to post
Share on other sites
i use

if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $emailfield))

to check my email field.... works well...

Share this post


Link to post
Share on other sites

×

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.