0

What would be the best method using jQuery to validate a field to ensure it ends in a specific TLD? Say I want it to only validate the email field if it ends in .edu?

**edit: just realized jQuery has it's own built-in validation, and not sure if I even need the bassistance plugin?

Joe
  • 5,955
  • 2
  • 29
  • 42
  • **quote:** _"just realized jQuery has it's own built-in validation..."_ You are mistaken. You would use the above mentioned plugin. – Sparky Jan 10 '12 at 01:58
  • You may have been reading http://docs.jquery.com/Plugins/Validation which is actually all about the plugin you referenced (note the "Plugins" in the name). – David Brainer Jan 10 '12 at 14:13

1 Answers1

2

I am not too familiar with that particular plugin, but the it looks like you should be able to extend it with syntax like the following:

jQuery.validator.addMethod("emailOrg", function(email_org, element) {
    email_org = email_org.replace(/\s+/g, ""); 
    return this.optional(element) || /^((([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])))\.)+org$/i.test(email_org);
}, "Please specify a valid .org email");

$("#myform").validate({
  rules: {
    field: {
      required: true,
      emailOrg: true
    }
  }
});

That example would validate all emails that end in org and no others.

David Brainer
  • 6,223
  • 3
  • 18
  • 16