Jump to content

[SOLVED] Help Understanding Validation


bassguru

Recommended Posts

Hello everyone,

 

I am currently using an email validater for my site. But I need some help understanding how it works...

 

$regexp="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})^";

 

The code works, but I just don't understand some of the components to the code! Could someone please explain to me how it works?

 

Many thanks

Bassguru

Link to comment
Share on other sites

A complete explanation provided by regexbuddy (get this program, it is golddust). You can see there are errors in the pattern.

^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})^

Assert position at the start of the string «^»
Match a single character present in the list below «[_a-z0-9-]+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   The character "_" «_»
   A character in the range between "a" and "z" «a-z»
   A character in the range between "0" and "9" «0-9»
   The character "-" «-»
Match the regular expression below and capture its match into backreference number 1 «(\.[_a-z0-9-]+)*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Note: You repeated the backreference itself.  The backreference will capture only the last iteration.  Put the backreference inside a group and repeat that group to capture all iterations. «*»
   Match the character "." literally «\.»
   Match a single character present in the list below «[_a-z0-9-]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      The character "_" «_»
      A character in the range between "a" and "z" «a-z»
      A character in the range between "0" and "9" «0-9»
      The character "-" «-»
Match the character "@" literally «@»
Match a single character present in the list below «[a-z0-9-]+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   A character in the range between "a" and "z" «a-z»
   A character in the range between "0" and "9" «0-9»
   The character "-" «-»
Match the regular expression below and capture its match into backreference number 2 «(\.[a-z0-9-]+)*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Note: You repeated the backreference itself.  The backreference will capture only the last iteration.  Put the backreference inside a group and repeat that group to capture all iterations. «*»
   Match the character "." literally «\.»
   Match a single character present in the list below «[a-z0-9-]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      A character in the range between "a" and "z" «a-z»
      A character in the range between "0" and "9" «0-9»
      The character "-" «-»
Match the regular expression below and capture its match into backreference number 3 «(\.[a-z]{2,3})»
   Match the character "." literally «\.»
   Match a single character in the range between "a" and "z" «[a-z]{2,3}»
      Between 2 and 3 times, as many times as possible, giving back as needed (greedy) «{2,3}»
Assert position at the start of the string «^»

Link to comment
Share on other sites

Nice tip Neil.

 

bassguru,

 

If I may, I think your pattern has some holes. For one, it does not allow the plus sign in the username, which is perfectly valid for an email address. Also, for the TLD you are only accepting 2 or 3 letter values. There are plenty of valid TLDs that are more than 3 characters in length (.MUSEUM and .TRAVEL being the longest ones I know of).

 

Here is the function I use for email validation. It validates the format and the length. Feel free to point out any flaws:

 

//=====================================================
// Function: is_email ( string $email )
//
// Description: Finds whether the given string variable
//              is a properly formatted email.
//
// Parameters: $email the string being evaluated
//
// Return Values: Returns TRUE if $email is valid email
//                format, FALSE otherwise.
//=====================================================

function is_email($email) {

    $formatTest = '/^[-\w+]+(\.[-\w+]+)*@[-a-z\d]{2,}(\.[-a-z\d]{2,})*\.[a-z]{2,6}$/i';
    $lengthTest = '/^(.{1,64})@(.{4,255})$/';

    return (preg_match($formatTest, $email) && preg_match($lengthTest, $email));

}

// NOTES:
//
// Format test
// - Username accepts: 'a-z', 'A-Z', '0-9', '_' (underscore), '-' (dash), '+' (plus), & '.' (period)
//       Note: cannot start or end with a period (and connot be in succession)
// - Domain accepts: 'a-z', 'A-Z', '0-9', '-' (dash), & '.' (period)
//       Note: cannot start or end with a period (and connot be in succession)
// - TLD accepts: 'a-z' & 'A-Z'
//
// Length test
// - Username: 1 to 64 characters
// - Domain: 4 to 255 character

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.