SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Need form to accept capitals

    Hi all, in the code below, does anyone know how to allow capital letters in the email address please?
    Code:
    <?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

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Try adding A-Z whereever there is a-z

  3. #3
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So replacing all a-z's with A-Z's ? But would that allow lower case as well?

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,070
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    No, replace a-z with a-zA-Z
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    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.

  6. #6
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  7. #7
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doing a DNS lookup will be slower than not doing one, but may also help filter non-existent email addresses.
    mikehealy.com.au
    diigital.com art, design . Latest WorkSaturday Morning

  8. #8
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    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)

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP: checkdnsrr - Manual crikey, I did not know that existed ...

    thx!

  11. #11
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    PHP: checkdnsrr - Manual crikey, I did not know that existed ...
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •