views:

39

answers:

3

I have a textarea that users enter e-mail addresses into separating them by commas. I am looking for a jquery plugin or snippet that will do live validation on this field so that users have to enter this properly.

Any ideas?

Thanks

A: 

This is a fairly specialized task; it's likely you'll have to roll your own code on this one.

Ben Alpert
+1  A: 

You could start with jquery/Plugin/Validation and create a custom validator plugin that does something along the lines of:

var values = value.split(/\s*,\s*/);
for (var i=0; i<values.length;i++) { 
  var testing = values[i];
  // test using the builtin e-mail validator
}
gnarf
A: 

I ended up writing my own function to do this. It takes the string form the field as an input and builds an array of e-mail addresses, validating each one for proper format.

$.fn.emailArrayFromCSL = function(csl){
  var valid = true;
  var result,value;
  csl = csl.replace(/;/g,",");
  csl = csl.replace(/ /g,",");
  csl = csl.replace(/,,/g,",");
  csl = csl.replace(/,,/g,",");
  email_array = csl.split(",");
  $.each(email_array,function(i,value){
    result = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);  
    valid = result && valid;
  });

  if(!valid){
    return false;
  }else{
    return email_array;
  }  
}

Currently it doesn't accept the format like - First Last [email protected]

Can anyone help me change this function to check which format (e-mail only or name+email) and when the latter, pull the e-mail out of that format and add it to the array?

ChrisH