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.

Related Posts Plugin for WordPress, Blogger...