SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating email addresses

    I'm using the jquery.validate.pack.js plugin to validate my contact form. I want to block certain email address account such as Hotmail, Outlook and Live from being validated. How can I add this rule to the validation? I have this line of code which validates the email address

    Code:
    PHP Code:
    if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$"trim($_POST['email']))) 
    Can I simply add the email address I want to not allow into the expression and if so where? I realise that eregi is now depreciated and I will update it.

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,395
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi freakystreak,

    Are you saying you want Hotmail address to fail validation, or you want them to pass validation without using the regex?

  3. #3
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want any hotmail address to fail validation

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,395
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    OK, so assuming that your email validation code has to return true or false, you could do it like this:
    PHP Code:
    $blacklist = array(
        
    'hotmail.com',
        
    'outlook.com'
        'live.com'  
    );

    foreach (
    $blacklist as $domain) {
        if (
    strpos($email$domain) >= 0) {
            return 
    FALSE;
        }
    }

    return (bool) 
    preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$/i"$email); 
    Basically you have a blacklist of the domains that you want to fail validation, and you loop through each one checking to see if it's present in the email address string.

  5. #5
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that will give it a try. Is there a way to get custom error messages with the jquery.validate.pack.js so I can let the user know that I don't accept Hotmail? Appreciate the help


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
  •