jQuery Validate, need to prevent free email addresses (e.g. Gmail, Hotmail)

前端 未结 3 706
抹茶落季
抹茶落季 2020-12-21 07:36

I need to add a custom validation rule (or set of rules) to prevent a whole list of email addresses from registering. This is already running server side, but we want to hav

相关标签:
3条回答
  • 2020-12-21 08:04

    Use your server-side array to generate a jQuery array containing the same values, then use jQuery's .inArray() method to look for it. http://api.jquery.com/jQuery.inArray/

    For example:

    $('#submit_button').click(function(event){
      var emailAddress = $('#email').val();
      var emailDomain = emailAddress.substr(emailAddress.search('@') + 1)
    
      if (jQuery.inArray(emailDomain, invalidAddresses))
      {
        alert("Invalid Email Address");
        event.preventDefault();
      }
    });
    
    0 讨论(0)
  • 2020-12-21 08:14

    If you already have this running server-side, just make an AJAX request back to your server to reuse the same validation logic. (Your client code will make a XMLHttpRequest back to the server, the server's validation logic runs, and returns the status to the client - all without requiring the HTML page to reload. You've already indicated that you're using jQuery, so this should be easy.)

    As an added bonus, your server-side code could cache the check - so that when the server repeats the check (probably only a few seconds later) for verification without client-involvement (necessary for security, as anything sent by the client can't be trusted - think Firebug, etc.) - the server may not need to repeat the full work of its check. (This may be too trivial to optimize, without knowing exactly what your server-side validation includes. If it includes any external calls to web services, etc., it is probably worth caching.)

    0 讨论(0)
  • 2020-12-21 08:18

    Why you don't make un array with all emails that you don't need.

    For example.

    var email = new Array('Hotmail','Gmail','Yahoo',...,'Live');
    

    how you already has all email list, make an array or an json file, xml, txt,...,etc

    Next. Make an function handdler with regular expression.

    $(function()
    
    //  $('selector').event(function(){ 
    
    function handdlerMail(email){
               for(var i = 0; i<email.length; i++){
                     if(evalEmail(current_value,/^email[i]/g){
                           // do something
                            } else  {  // do something } 
                } // end for
    } // End function 
    
    function evalEmail(a,b) {
          if ((b.test(a))) {
                return true;
           } else {
                return false;
        }
    }
    
    }) // end of event
    
    }) // enf of function
    

    I think a better way of doing this with a tree structure of weights, ie if you could create a tree which contains, emails do not want.

    then you could be evaluating your emails in logarithmic time. and at the end of walking the tree weights, you could decide whether or not it in your list of aceepted.

    ie whether or not it in the tree

    0 讨论(0)
提交回复
热议问题