Wednesday, July 8, 2009

Improved email validation, plus multiple emails validation

jQuery validation plugin .. was causing ff2 to choke. It also didn't have a multiple-email validator .. or a split-up phone validator -- although on that last one, I highly advise you push back - a phone # field should just be ONE INPUT.


// http://docs.jquery.com/Plugins/Validation/Methods/email
email: function(value, element) {
// contributed by Dennis Hall
// simplified enough for ff2 to not choke
return this.optional(element) || /^\s*[^ <>]+@[^ <>]+\.[a-z]{2,9}\s*$/i.test(value);
},
email_multiple: function(value, element) {
// contributed by Dennis Hall
// simplified enough for ff2 to not choke
var length = value.length,
indexOfSeparator = -1,
isValid = true;
///
for(var i=0;i<length;i++){
var ch = value.charAt(i);
if( ch == ',' || ch == ';' || i == length-1){
isValid = isValid && /^\s*[^ <>]+@[^ <>]+\.[a-z]{2,9}\s*$/i.test(value.substring(indexOfSeparator+1, (i==length-1?length:i)));
indexOfSeparator = i;
}
}
return this.optional(element) || isValid;
},
phone3: function(value, element) {
// contributed by Dennis Hall
// first field must be of the form "<field name id>-1", and other 2 fields must be of the form "<field name id>-2", "<field name id>-3"
var idBase = element.id.replace(/-1$/,''),
values = [value, $('#'+idBase+'-2').val(), $('#'+idBase+'-3').val()],
requiredLengths = [3,3,4];
///
for(var i=0;i<3;i++){
if(!/^\d+$/.test(values[i]) || values[i].length != requiredLengths[i]){
return false;
}
}
return true;
},

No comments:

Post a Comment