简体   繁体   English

如何根据电子邮件地址域/ TLD验证jQuery中的表单字段? (.com,.net,.edu?)

[英]How to validate a form field in jQuery based on email address domain / TLD? (.com, .net, .edu?)

What would be the best method using jQuery to validate a field to ensure it ends in a specific TLD? 使用jQuery验证字段以确保其在特定TLD中结束的最佳方法是什么? Say I want it to only validate the email field if it ends in .edu ? 假设我希望它只验证电子邮件字段,如果它以.edu结尾

**edit: just realized jQuery has it's own built-in validation, and not sure if I even need the bassistance plugin ? **编辑:刚刚意识到jQuery有自己的内置验证,不确定我是否需要bassistance插件

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. 该示例将验证以org结尾的所有电子邮件,而不验证其他电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM