Re: Poll of sorts: Javascript Form validation or PHP

From: Date: Sun, 07 Dec 2008 13:59:51 +0000
Subject: Re: Poll of sorts: Javascript Form validation or PHP
References: 1 2 3 4 5 6 7 8  Groups: php.general 
Request: Send a blank email to php-general+get-284255@lists.php.net to get a copy of this message
/** Validate an email address. Provide email address (raw input) Returns true if the email address has the email address format and the domain exists. Not following the usual coding style I know but I can't bring myself to touch it... You'd better not touch it either, Rob: That means you. Author: Humblehope Modified: 06/06/2007 **/ function validEmail($email) { $isValid = true; $atIndex = strrpos($email, "@"); if (is_bool($atIndex) && !$atIndex) {
	      		$isValid = false;
} else {
      			$domain = substr($email, $atIndex+1);
      			$local = substr($email, 0, $atIndex);
      			$localLen = strlen($local);
      			$domainLen = strlen($domain);
      			if ($localLen < 1 || $localLen > 64)
      			{
         			// local part length exceeded
         			$isValid = false;
      			}
      			else if ($domainLen < 1 || $domainLen > 255)
      			{
        			// domain part length exceeded
        			$isValid = false;
      			}
      			else if ($local[0] == '.' || $local[$localLen-1] == '.')
      			{
        			// local part starts or ends with '.'
         			$isValid = false;
      			}
      			else if (preg_match('/\\.\\./', $local))
      			{
        			// local part has two consecutive dots
         			$isValid = false;
      			}
      			else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      			{
        			// character not valid in domain part
         			$isValid = false;
      			}
      			else if (preg_match('/\\.\\./', $domain))
      			{
        			// domain part has two consecutive dots
        		 	$isValid = false;
      			}
      			else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
      			{
        			// character not valid in local part unless
        			// local part is quoted
         			if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local)))
         			{
            				$isValid = false;
         			}
      			}
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      			{
        			// domain not found in DNS
         			$isValid = false;
      			}
} return $isValid; } On Sun, 7 Dec 2008, Nathan Rixham wrote:
Ashley Sheridan wrote:
On Sun, 2008-12-07 at 02:27 -0800, Yeti wrote:
I put a small one together using regular expressions, http://www.ashleysheridan.co.uk/coding_php_validation.php
So we are "regexing" emails again? #OUT OF coding_php_validation.php COPY case 'email': { $expression = "/^([a-z0-9_\-\.]+)@([a-z0-9_\-\.]+)\.([a-z]{2,5})$/i"; $errorText = "The email does not appear to be a valid type."; break; } #END COPY What should be valid email addresses according to RFC 2822 [1]: !#$%&*+-/=?^_`{|}~@example.com "@"@example.com Not valid email addresses: "\"@example.com @@example.com - -@example.com Valid email addresses according to the Multipurpose Internet Mail Extension (MIME) [2]: ä@example.com Ã(c)@℞.com
Yes, I know I'm going to hell, but it serves well for most purposes. In the future when I actually see people use those kind of email addresses then I'll update it. Ash www.ashleysheridan.co.uk
basically if it has 1 or more printable chars followed by an @ followed by 1 - 63 printable chars followed by a period followed by a valid domain extension it's good; that's simple regex :p -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#284255) next »