Need form to accept capitals

Hi all, in the code below, does anyone know how to allow capital letters in the email address please?

<?php
if($_POST["formsent"] == "formsent")
{
if ($_POST['send']) { 
  extract($_POST); 
  $errors=""; 
  $format = "/^[-_a-z0-9]+(\\.[-_a-z0-9]+)*@[-a-z0-9]+(\\.[-a-z0-9]+)*\\.[a-z]{2,6}$/i"; 
  if (!preg_match("/[-_a-z0-9.@]/i",$email) || !preg_match($format,$email)) { 
    $errEmail=1; 
  } 

Any help appreciated.

Dez

uhm since you’re checking an e-mail, you should probably be using filters instead of regex.
PHP: filter_var - Manual

Which will return false if it doesn’t pass the character test. Over on another forums a group of us came up with this function for testing an e-mail address – it’s about as robust and bulletproof as you’re gonna get:


function common_isValidEmail($address) {
	/* check namespace */
	if (filter_var($address,FILTER_VALIDATE_EMAIL)==FALSE) {
		return false;
	}
	/* explode out local and domain */
	list($local,$domain)=explode('@',$address);

	$localLength=strlen($local);
	$domainLength=strlen($domain);

	return (
		/* check for proper lengths */
		($localLength>0 && $localLength<65) &&
		($domainLength>3 && $domainLength<256) &&
		(
			/* is valid/registered domain name? */
			checkdnsrr($domain,'MX') ||
			checkdnsrr($domain,'A')
		)
	);
}

Since it uses filter_var to check the character space, explodes to test the length of the local and domain against the specification limits, AND checks that the domain name is in fact a valid/registered one with a DNS lookup.

Hope this helps.

So replacing all a-z’s with A-Z’s ? But would that allow lower case as well?

No, replace a-z with a-zA-Z :slight_smile:

Try adding A-Z whereever there is a-z

I’ve been reading 3 pages of PHP.NET a night for about two years. I figure in another three years I’ll have read the entire manual… I’m still finding out about functions I’ve never heard of in there… For example right now I’m trying to get a handle on how reflections work.

One of the few reasons PHP is viable despite being an interpreted language (I don’t see people lining up to use ROM based BASIC for example) – library functions (likely written in C and compiled to ASM) will always be faster than user code (interpreted) – so having a massive function library to handle anything you could want to do makes a lot of sense from a speed perspective.

SO many times the past couple years I’ve seen cases of people brute force coding things PHP already has functions to handle.

PHP: checkdnsrr - Manual crikey, I did not know that existed …

thx! :slight_smile:

The DNS lookup is indeed the only part that might take a bit – but it doesn’t really take that long.

You’d be surprised how many spammers don’t even bother using a free mail service or legitimate mail name.

But yeah, that could be shortened to this:


function common_isValidEmail($address) {
	/* check namespace */
	if (filter_var($address,FILTER_VALIDATE_EMAIL)==FALSE) {
		return false;
	}
	/* explode out local and domain */
	list($local,$domain)=explode('@',$address);

	$localLength=strlen($local);
	$domainLength=strlen($domain);

	return (
		/* check for proper lengths */
		($localLength>0 && $localLength<65) &&
		($domainLength>3 && $domainLength<256)
	);
}

I’d still keep the length checks – filter_var checks for valid characters, but will pass invalid lengths.

Really though – I’d probably not yank the DNS checks unless your server’s DNS lookups are ridiculously slow for some reason. (I was on a host like six or seven years ago that was so afflicted)

Doing a domain lookup is pretty much pointless. Just use the filter_var function and leave it at that. If someone really wanted to use a fake email address, they could just put any hotmail address and theres nothing you could do to prove it does not belong to them.

Doing a DNS lookup will be slower than not doing one, but may also help filter non-existent email addresses.

Thanks to you both for the help on this - it’s appreciated. deathshadow60 - would that code slow the page down at all? Any downsides to it?