SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I exclude/block yahoo.com domain during jquery form validation?

    I'm new to jQuery and hope someone can point me in the right direction. I'm tring to validate a web form and return a message to the visitor that a business address is required. I would like to block yahoo, hotmail, gmail and aol domains. I found the 'accept:' method, but is there an "exclude:' or something simular? here is the code I have:

    Code:
    $(document).ready(function () {                                                                                        
      $("#webform").validate({
        errorPlacement: function(error, element) {
          error.appendTo( element.parents("div.field:first").find("div.clear:first") );                                                        
        },
        onfocusout: false,
        onkeyup: false,
        onclick: false,
        debug: false,
        rules: {
            email_field: {
                required: true,
                email: true,
                accept: 'yahoo.com'
            }
        } // rules
      }); // validate
    }); // function
    Last edited by Force Flow; Nov 30, 2012 at 16:09. Reason: added code tags

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,997
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    And welcome to the forums.

    I'm unaware of an "exclude" method, but it would be quite easy to code this up by hand.

    This is what I would do:

    Code JavaScript:
    $(document).ready(function(){
      // Custom validation rule
      $.validator.addMethod("checkDomain", function(value, element) {
        var evilDomains = ["yahoo", "gmail", "aol", "hotmail"];
        var domainEntered = value.replace(/.*@(.*)\..*/, "$1");
        var isAcceptableDomain = true;
     
        if($.inArray(domainEntered, evilDomains) >= 0){
          isAcceptableDomain = false;
        }
          return this.optional(element) || (isAcceptableDomain);
      }, "Please use a sensible mail address");    
     
     $("#webform").validate({
        errorPlacement: function(error, element) {
          error.appendTo( element.parents("div.field:first").find("div.clear:first") );                                                        
        },
        onfocusout: false,
        onkeyup: false,
        onclick: false,
        debug: false,
        rules: {
          email : { checkDomain: true }
        } // rules
      }); // validate
    }); // function

    Hopefully the code is quite straight forward, but if you have any questions, just let me know.


Tags for this Thread

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
  •