使用 RSV jquery 插件验证来自特定域的电子邮件

发布于 2024-10-28 22:03:17 字数 836 浏览 5 评论 0原文

我正在尝试使用 Ben Keen 的 RSV Jquery 插件来验证通过注册表提交的电子邮件地址:仅允许用户提交属于特定域的电子邮件地址。我认为使用插件的 reg_exp 规则可能是一个解决方案,但我无法让它工作。 我是 jquery 新手,所以我可能犯了一些非常愚蠢的错误:有人能指出我正确的方向吗?

这是我尝试使用的代码,改编自 Keen 的演示之一:

<script type="text/javascript">

  // a custom onComplete handler to prevent form submits for the demo
  function myOnComplete()
  {
    alert("The form validates! (normally, it would submit the form here).");
    return false;
  }

    var rules = [];

// standard form fields

rules.push("reg_exp,reg_exp_field1,/@mydomain\.tld$/gi,Please enter your valid e-mail (e.g. \"@mydomain\.tld\")");


$(document).ready(function() {
    $("#demo_form3").RSV({
    onCompleteHandler: myOnComplete,
    rules: rules
    });
}); 
</script>

使用上面的代码,当我提交有效的电子邮件地址(即以 @mydomain.tld 结尾的地址)时,我总是会收到警报。我做错了什么?

I'm trying to use Ben Keen's RSV Jquery plug-in to validate an e-mail address submitted through a registration form: users are only allowed to submit an e-mail address that belongs to a specific domain. I thought that using the plugin's reg_exp rule could be a solution, but I can't get it to work.
I'm a jquery newbie, so I'm probably making some very silly mistake: can someone point me to the right direction?

Here's the code I'm trying to use, adapted from one of Keen's demo:

<script type="text/javascript">

  // a custom onComplete handler to prevent form submits for the demo
  function myOnComplete()
  {
    alert("The form validates! (normally, it would submit the form here).");
    return false;
  }

    var rules = [];

// standard form fields

rules.push("reg_exp,reg_exp_field1,/@mydomain\.tld$/gi,Please enter your valid e-mail (e.g. \"@mydomain\.tld\")");


$(document).ready(function() {
    $("#demo_form3").RSV({
    onCompleteHandler: myOnComplete,
    rules: rules
    });
}); 
</script>

Using the code above, when I submit a valid e-mail address, that is an address ending with @mydomain.tld I always get an alert. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽蝶幻影 2024-11-04 22:03:17

我已经为很多行代码编写了 RSV。它非常强大。
然而,你用错了它。

  1. 您应该定义一个自定义函数,而不是其默认的电子邮件格式验证方法。
  2. 您应该将上面提到的“自定义函数”放入验证数组中,但不要将其放入“myOnComplete”方法中,该方法仅在所有验证通过时调用。
  3. 在 RSV 中,自定义函数仅应返回“true”(通过时)或“数组”(失败时)。但在上面的示例中,您实现为“return false”。

不管怎样,你已经用另一种方式解决了你的问题。 :-) 那挺好的。

I have written RSV for many lines of code. It's very powerful.
However, you used it incorrectly.

  1. you should define a customized function instead of its default email-format validation method.
  2. you should put that "customized function" mentioned above into the validation array, but not put it in "myOnComplete" method, this method only called when all the validations are passed.
  3. in RSV, a customized function only should return "true"(when passed) or "an Array"(when failed). but in the example above, you implemented as "return false".

Whatever, you have solved your problem in another way. :-) that's good.

梓梦 2024-11-04 22:03:17

没关系,经过进一步的研究,我找到了一个更简单的解决方案:遵循

  $(document).ready(function(){

$.validator.addMethod("gmail", function(value, element) {
    return this.optional(element) || /^[\w-\.]+@(gmail\.com|yahoo\.com)+$/i.test(value);
}, "Email address must contain gmail or yahoo addresses."); 

    $("#register_member_form").validate();
});

Never mind, after further researche I found a simpler solution: following the advice in this forum thread I used jqueryValidate instead of rsv plug-in and added custom validate method.
I'm posting here my solution, maybe it could be useful for some other jquery newbie like me!

  $(document).ready(function(){

$.validator.addMethod("gmail", function(value, element) {
    return this.optional(element) || /^[\w-\.]+@(gmail\.com|yahoo\.com)+$/i.test(value);
}, "Email address must contain gmail or yahoo addresses."); 

    $("#register_member_form").validate();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文