3

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 have the front-end mimic this as well.

I have a large array of 40 or 50 free email accounts (e.g. Gmail, Hotmail, Live mail, etc) and need to have any email addresses using one of the stems from the list validate as FALSE.

How might I go about doing this?

Sparky
  • 98,165
  • 25
  • 199
  • 285
Jack McDade
  • 689
  • 7
  • 14
  • 3
    Off topic, but why do you want to prevent people registering with a free email account? I know people whose _only_ email address is a free one. – nnnnnn Jan 15 '12 at 05:36
  • 1
    It's a Business to Business web app the involves validating as an employee/partner in a real, physical company. Like Apple.com, for example. – Jack McDade Jan 15 '12 at 05:52
  • 1
    OK, fair enough. Except do employees at, say, Yahoo have addresses that look different to free Yahoo addresses? – nnnnnn Jan 15 '12 at 06:00
  • Getting pretty semantic here, but in that case we would have to manually give them access. – Jack McDade Jan 15 '12 at 06:04

3 Answers3

4

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.)

ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • Your answer is much better than mine; so I'm getting rid of mine. – Dhaivat Pandya Jan 15 '12 at 05:05
  • I'm not the server-side guy in this project, so moving their code around to get access to XMLHttpRequest isn't feasible in the timeframe i have. But I like your idea. I need a more-or-less rough and dirty solution. – Jack McDade Jan 15 '12 at 05:32
  • 1
    @JackMcDade - do you have a the "rules" as to how a free email address is determined? Maybe it is as simple as a blacklist of domains? Given these requirements, I'm sure someone can provide a JavaScript-only implementation. Otherwise, there's also probably a 3rd-party web service that you could call - without making the implementation that already exists on your server available as one. – ziesemer Jan 15 '12 at 05:35
  • Yes it's just a static, blacklist. I have a pretty little js array waiting for me to run it through the right validation method. – Jack McDade Jan 15 '12 at 05:51
1

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();
  }
});
njbair
  • 1,982
  • 16
  • 14
0

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

exbios
  • 49
  • 1
  • 3
  • Please don't use `for(i in email)`. See http://stackoverflow.com/questions/500504/javascript-for-in-with-arrays. – ziesemer Jan 15 '12 at 14:14