Wed Aug 18 17:29:21 EEST 2010

How to validate a form email address with PHP / PHP Validate Email address

I'm writting a small contact form from scratch and I needed a quick and dirty way to validate an email address with PHP

I've googled to find some examples and I found many examples for completing the task, however in order to validate the email address I eventually I ended up with the following PHP code:

         if ( preg_match( "/^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/", $email ) ) 
                      {
                      # Do some processing here - input if valid
                      $valid_email = '1';
                      }
         else
                      {
        print "" . 'Loan Conservator Logo' . '

Bad e-mail address. Please correct it '. 'and resubmit the form

 
'; exit (1); }


Now just pass the input $email variable from your form to the if condition and modify according to your needs.