jo.nova Posted July 10, 2006 Share Posted July 10, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/14180-php-form-validation/ Share on other sites More sharing options...
designationlocutus Posted July 10, 2006 Share Posted July 10, 2006 Have a look into the [b]ereg()[/b] function over at php.net. If you're doing form validation, it might be worth you looking into regular expressions. Quote Link to comment https://forums.phpfreaks.com/topic/14180-php-form-validation/#findComment-55574 Share on other sites More sharing options...
Daniel0 Posted July 10, 2006 Share Posted July 10, 2006 In my opinion [url=http://php.net/preg_match]preg_match[/url] is better. Quote Link to comment https://forums.phpfreaks.com/topic/14180-php-form-validation/#findComment-55576 Share on other sites More sharing options...
Ninjakreborn Posted July 10, 2006 Share Posted July 10, 2006 [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 scriptthen 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 likesomething@something.comthen it returns false/ Quote Link to comment https://forums.phpfreaks.com/topic/14180-php-form-validation/#findComment-55578 Share on other sites More sharing options...
designationlocutus Posted July 10, 2006 Share Posted July 10, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/14180-php-form-validation/#findComment-55580 Share on other sites More sharing options...
Daniel0 Posted July 10, 2006 Share Posted July 10, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/14180-php-form-validation/#findComment-55583 Share on other sites More sharing options...
designationlocutus Posted July 10, 2006 Share Posted July 10, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/14180-php-form-validation/#findComment-55585 Share on other sites More sharing options...
Ninjakreborn Posted July 10, 2006 Share Posted July 10, 2006 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 isOne 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 Quote Link to comment https://forums.phpfreaks.com/topic/14180-php-form-validation/#findComment-55592 Share on other sites More sharing options...
designationlocutus Posted July 10, 2006 Share Posted July 10, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14180-php-form-validation/#findComment-55595 Share on other sites More sharing options...
brown2005 Posted July 10, 2006 Share Posted July 10, 2006 i use if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $emailfield))to check my email field.... works well... Quote Link to comment https://forums.phpfreaks.com/topic/14180-php-form-validation/#findComment-55602 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.