Jump to content


Photo

PHP form validation


  • Please log in to reply
9 replies to this topic

#1 jo.nova

jo.nova
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 10 July 2006 - 02:36 PM



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?


#2 designationlocutus

designationlocutus
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 10 July 2006 - 02:46 PM

Have a look into the ereg() function over at php.net. If you're doing form validation, it might be worth you looking into regular expressions.

#3 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 10 July 2006 - 02:48 PM

In my opinion preg_match is better.

#4 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 10 July 2006 - 02:50 PM

$regex = "^[A-Za-z0-9\._-]+@([A-Za-z0-9][A-Za-z0-9-]{1,62})(\.[A-Za-z][A-Za-z0-9-]{1,62})+$";
Make that a variable that can be accessed by the script
then pass this into the script
if (!ereg("$regex", $_POST['email'])) { //tests emails format with regex variable above
		$errorhandler .= "The email address is improperly formatted<br />";
	}
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/

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#5 designationlocutus

designationlocutus
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 10 July 2006 - 02:51 PM

In my opinion preg_match is better.

Hmm what are a major advantages over each?

#6 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 10 July 2006 - 02:56 PM

In my opinion preg_match is better.

Hmm what are a major advantages over each?


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

#7 designationlocutus

designationlocutus
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 10 July 2006 - 03:00 PM

In my opinion preg_match is better.

Hmm what are a major advantages over each?


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


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

#8 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 10 July 2006 - 03:13 PM

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

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#9 designationlocutus

designationlocutus
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 10 July 2006 - 03:16 PM

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.

#10 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 10 July 2006 - 03:30 PM

i use

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

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users