Search

Wednesday, March 16, 2011

email validation using php

Form validation is must to prevent spam and form attacks. Email validation is also must to making a form. We use Regular Expressions for validation in PHP, which is secure and highly usable on web. preg_match() and ereg() functions are using for validation. But sometimes ereg() function produce some Deprecated Error. To prevent Deprecated Error we use preg_match for email validation. Here are some codes for Email Validation :

function email_Validate($str)
 {
   if(preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $str)) 
   {
  return true;
   }
 }
 

and now we use this email validation function in our form

if(!email_Validate($email))
 {
  echo "Email not valid!"; 
 }

Beside PHP you can validate your email with javascript/jquery. Its quite easy to implement and use with the help of javascript/jquery. Here is my code for jquery and you can also implement these with javascript :

var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

//userMail is a input box name 
var email = $('#userMail').val();

if(reg.test(email) == false)
{
   alert('Email Not Valid');
}

this validation help you to prevent spamming in login system or registration form.

4 comments:

SHAKHER DEVAL said...

nice dude nice... its a really a good job by you...

keep it up boy ........

james said...

really nice work dude, i always stuck my head with to understand preg_match() strategy, and you give a nice idea for email validation in php as well as jquery. But i thing there is not huge difference in preg_match() and ereg_match().

anyway thanks for this tutorial.

Anonymous said...

Great blog at least I think so. Thanks a lot for posting this information.

Joseph Writeman
cell jamming

Anonymous said...

Great post, sweetie! Keep it up! Almost forgot, why don't you make your website a bit more social.


Barbara Martin
dominos coupon codes

Related Posts Plugin for WordPress, Blogger...