Solved

How do i validate length of entire regex?

Posted on 2012-03-21
10
236 Views
Last Modified: 2012-03-22
I am trying to build a regex to check an email address

/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/

works fine, but i only want it to accept up to 256 chars.

something like

/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*${3,254}/


but ofcourse it does not work, how do i do this?
0
Comment
Question by:tim_carter
10 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37749335
Try:

/^(?=.{3,256}$)\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37749502
You might find this function useful.
<?php // RAY_email_validation.php
error_reporting(E_ALL);


// A FUNCTION TO TEST FOR A VALID EMAIL ADDRESS, RETURN TRUE OR FALSE
// SEE MAN PAGE: http://php.net/manual/en/intro.filter.php
function check_valid_email($email)
{
    // FROM THE POST AT EE, CONSIDER ONLY 256 CHARACTERS
    $email = substr(trim($email),0,256);
    
    // LIST OF BLOCKED DOMAINS
    $bogus = array
    ( '@unknown.com'
    , '@example.com'
    , '@gooseball.org'
    )
    ;

    // IF PHP 5.2 OR ABOVE, WE CAN USE THE FILTER
    if (strnatcmp(phpversion(),'5.2') >= 0)
    {
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) return FALSE;
    }

    // IF LOWER-LEVEL PHP, WE CAN CONSTRUCT A REGULAR EXPRESSION
    else
    {
        $regex
        = '/'                        // START REGEX DELIMITER
        . '^'                        // START STRING
        . '[A-Z0-9_-]'               // AN EMAIL - SOME CHARACTER(S)
        . '[A-Z0-9._-]*'             // AN EMAIL - SOME CHARACTER(S) PERMITS DOT
        . '@'                        // A SINGLE AT-SIGN
        . '([A-Z0-9][A-Z0-9-]*\.)+'  // A DOMAIN NAME PERMITS DOT, ENDS DOT
        . '[A-Z\.]'                  // A TOP-LEVEL DOMAIN PERMITS DOT
        . '{2,6}'                    // TLD LENGTH >= 2 AND =< 6
        . '$'                        // ENDOF STRING
        . '/'                        // ENDOF REGEX DELIMITER
        . 'i'                        // CASE INSENSITIVE
        ;
        // TEST THE STRING FORMAT
        if (!preg_match($regex, $email)) return FALSE;
    }

    // TEST TO SEE IF THE DOMAIN IS IN OUR BLOCKED LIST
    foreach ($bogus as $badguy)
    {
        if (stripos($email, $badguy)) return FALSE;
    }

    // FILTER_VAR OR PREG_MATCH DOES NOT TEST IF THE DOMAIN IS ROUTABLE
    $domain = explode('@', $email);

    // MAN PAGE: http://php.net/manual/en/function.checkdnsrr.php
    if ( checkdnsrr($domain[1], "MX") || checkdnsrr($domain[1], "A") ) return TRUE;

    // EMAIL IS NOT ROUTABLE
    return FALSE;
}



// DEMONSTRATE THE FUNCTION IN ACTION
$e = NULL;
if (!empty($_GET["e"]))
{
    $e = $_GET["e"];
    if (check_valid_email($e))
    {
        echo "<br/>VALID: $e \n";
    }
    else
    {
        echo "<br/>BOGUS: $e \n";
    }
}


// END OF PROCESSING - CREATE THE FORM USING HEREDOC NOTATION
$form = <<<ENDFORM
<form>
TEST A STRING FOR A VALID EMAIL ADDRESS:
<input name="e" value="$e" />
<input type="submit" />
</form>
ENDFORM;

echo $form;

Open in new window

0
 
LVL 10

Expert Comment

by:pfrancois
ID: 37751267
If you accept to code two different regexes, you can first perform a test on the length with
/.{257}/

Open in new window

and then perform a text on the structure with your initial working regex
/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/

Open in new window


If the first expression matches, the address is too large.
0
 
LVL 84

Expert Comment

by:ozo
ID: 37751767
fred&barney@stonehenge.com
is a valid email address
0
 
LVL 1

Author Comment

by:tim_carter
ID: 37752214
/^(?=.{3,254}$)\w+([-+&.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/

This regex is perfect
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Closing Comment

by:tim_carter
ID: 37752217
Thanks.

/^(?=.{3,254}$)\w+([-+&.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/

Modified with & because that is legal in local part
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37752391
This regex is perfect
I am always amused when I see something like that in writing!  The internet is littered with regular expressions that do not quite work right.  Many of the worst offenders are regular expressions designed to tell if an email address is valid.  That is probably the most important reason for development of this function:
http://php.net/manual/en/function.filter-var.php

Filter_var() has been available since the years-ago release of PHP 5.2.  See:
http://php.net/manual/en/filter.filters.validate.php

PHP 5.2 is now obsolete and is not supported any more. PHP 5.2 is so old it is not even given security releases.  So this is a good time to reconsider whether writing your own regex to validate emails still makes sense.  It's kind of like adjusting a carburetor.  It doesn't really matter how good you are with a carburetor.  Carburetor adjustment is an obsolete skill.  We don't have to do that any more because we have fuel injected engines.  And we don't have to tinker with email validation because we have filter_var().

Filter_var() does not check whether the email is routable.  I choose to include that test in the function posted at ID: 37749502 on line 56.  If you're sending email to yourself at localhost, you might or might not want to include that functionality.
0
 
LVL 1

Author Comment

by:tim_carter
ID: 37752398
Hi Ray,

I actually use filter_var in php to check email addresses.

Im am just practicing on regex. I do not main that it is perfect to validate an email, because there is still emails it will not validate.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37752426
Filter_var() is the right tool.  

You might enjoy this article if you're learning about regular expressions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
0
 
LVL 1

Author Comment

by:tim_carter
ID: 37752438
Thanks ray, I will look at that article
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now