使用JQuery验证插件,我想更改远程验证方法 [英] With JQuery Validation Plugin, I want to change the remote validation method

查看:78
本文介绍了使用JQuery验证插件,我想更改远程验证方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery validate插件使用此插件的远程方法针对数据库验证电子邮件地址.

I'm using jquery validate plugin to validate an email address against a database using the remote method of this plugin.

我需要检查电子邮件地址是否不在我们的数据库中.最初,该脚本与服务器端代码结合使用时,只需检查数据库中的电子邮件地址并返回true或false,即可成功运行.

I need to check the email address is not in our database already. Originally, the script in combination with server side code simply checked for the email address in our database and returned true or false, this worked successfully.

我现在需要添加一个附加功能,因为现在只要记录的站点ID不同,我们就可以在数据库中拥有相同的电子邮件地址两次,即如果fred@bloggs.com的记录具有siteid = site1已存在于数据库中,您不能再将siteID = site1添加到fred@bloggs.com,但是可以将siteID = site2添加到fred@bloggs.com.

I now need to add an additional feature as it's now possible for us to have the same email address in the database twice as long as the siteid for the record is different i.e. if a record for fred@bloggs.com with siteid=site1 exists in the database, you can't add fred@bloggs.com with siteid=site1 again, but you can add fred@bloggs.com with siteid=site2.

我已经确认服务器端代码可以正常工作,并且对于给定的电子邮件地址和站点ID,它按预期返回true或false.

I've confirmed that the serverside code is working and for a given email address and siteid, it returns true or false as expected.

无论何时更改站点ID选择框,我都希望重新验证电子邮件字段.目前,似乎还记得它是以前的验证(它已经在加载时经过验证),并且不受站点ID更改的影响,除非我将电子邮件从fred@bloggs.com更改为其他电子邮件,然后再将其改回,然后它才能正确验证用于新的siteid.我尝试了删除规则和添加规则的组合,似乎没有任何效果.

I want to revalidate the email field whenever the siteid select box is changed. At the moment it seems to remember it's previous validation (it's already validated at load) and is not affected by the siteid change, unless I change the email from fred@bloggs.com to something else, then change it back, then it validates correctly for the new siteid. I've tried a combination of remove rules and add rules, nothing seems to work.

帮助!

<html>
<head>
<script type="text/javascript">
$(document).ready(function () {
    var enquiryform = {
        rules: {
       email: {
           required: true,
      email: true,
      remote: 'emailcheck2.asp?siteid=' + $('#siteid').val()
       }        
   },
   messages: {
            email: {
           remote: 'exists already!'
       }
        }           
    };

    var validatedform = $(".validatedform").validate(enquiryform).form();       
    $("#siteid").bind("change", function(e){
        enquiryform.rules.email.remote='emailcheck2.asp?siteid=' + $('#siteid').val();
    });
});
</script>
</head>
<body class="contactadd">
</head>
<body>
<form method="Post" class="validatedform">
<input class="email" id="email" name="email" type="text">
<select id="siteid" name="siteid">
<option value="1">Site 1</option>
<option value="2">Site 2</option>
</select>
<input id="submitbutton" type="submit" value="Create">
</form>
</body>
</html>

推荐答案

出于性能原因,该插件会缓存该值.作为目前的解决方法,您可以尝试在相关值更改时手动使缓存无效:

The plugin caches the value for performance reasons. As a workaround for now you could try to invalidate the cache manually whenever the dependent value changes:

$("#site").change(function() {
  $("#email").removeData("previousValue");
});

这篇关于使用JQuery验证插件,我想更改远程验证方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆