强制MVC3上不显眼的远程验证一个重新验证 [英] Forcing a revalidate on mvc3 unobtrusive remote validation

查看:174
本文介绍了强制MVC3上不显眼的远程验证一个重新验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个经典的登录流程。用户可以新用户或现有用户之间选择。如果用户是新的,在登录框中的名称应验证对服务器查看这个用户名是独一无二的,如果它是一个现有的用户,因为我们预期将已拍摄的用户名(ofcourse)这个检查将被跳过。

It's a classic login flow. The user can choose between 'new user' or 'existing user'. If the user is new, the name in the login box should validate against the server to see if the username is unique, if it's an existing user this check will be skipped since we expect the username to be taken already (ofcourse).

我添加的视图模型一个[远程]属性并且增加了单选按钮为新的/退出的用户为附加字段。通过这种方式,远程验证将只返回true,如果它是一个现有的用户,以及检查数据库,如果它不是,看看这个用户名是否服用。

I added a [Remote] attribute on the viewmodel and added the radiobutton for new/exiting user as 'additional fields'. This way the remote validation will just return true if it's an existing user, and check the database if it's not, to see if the username is taken.

这个伟大的工程,当用户决定改变单选按钮,除了(新/现有的)进入后输入用户名(与远程验证已运行)。因为当用户名的变化(这是使用[遥控]属性的属性),单独改变单选按钮,不会有远程验证只自动运行它再次运行。

This works great, except when the user decides to change the radiobutton (new/existing) after entered a username (and the remote validation has run). Since remote validation is only automatically run when the username changes (that's the property with [Remote] attribute), changing the radiobutton alone, will not have it run again.

所以我的问题是,我怎么能强迫远程验证,再次运行?我想一般的黑客通过触发上的用户名输入场的变化/焦点/模糊的事件,但呼叫不会被触发。我认为增加的单选按钮类似[远程],但真的有东西两个相等寻找错误信息,放置在同一个绝对位置复杂化。

So my problem is, how can i force the remote validation to run again? I tried the usual hacks by triggering a change/focus/blur event on the username input field, but the call is not triggered. I considered adding a similar [Remote] on the radiobutton, but that would really complicate things with two equal looking error messages, placed at the same absolute position.

有没有什么办法来触发一个再验证?

Is there any way to trigger a revalidation?

推荐答案

我相信jQuery验证可以用$(#formID)被触发。验证()

I believe that jquery validation can be triggered using $("#formID").validate()

有些选项可以在文档中找到: http://docs.jquery.com/Plugins/validation ,并在布拉德·威尔逊的博客(你也可以在注释中查找一些信息)

Some more options can be found in the docs: http://docs.jquery.com/Plugins/validation, and at Brad Wilson's blog (you can also find some info in the comments)

您看着数据属性,输入了,也许它的缓存?像<一个href=\"http://stackoverflow.com/questions/2710548/how-do-i-use-jquery-validate-remote-validation-on-a-field-that-depends-on-another\">this.

Have you looked into the Data attributes that the input has, maybe it's cached? something like this.

编辑:只是为了澄清,这是我如何得到它的工作。

Just for clarification, this is how i got it to work

$('#UserName').removeData('previousValue');
$('form').validate().element('#UserName');

这篇关于强制MVC3上不显眼的远程验证一个重新验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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