在文本框上手动设置不显眼的验证错误 [英] Manually set unobtrusive validation error on a textbox

查看:19
本文介绍了在文本框上手动设置不显眼的验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些类似于远程验证的事情,除了我已经通过 jquery 手动调用并设置了我必须设置的任何内容.

I'm doing something similar to the remote validation, except that I already make my calls manually via jquery and setup whatever I had to setup.

现在我的问题是,如果我想告诉验证器特定文本框无效(并阻止页面提交,突出显示文本框等).我将如何从代码中做到这一点?

Now my problem is, if I want to tell the validator that a particular textbox is not valid (and prevents the page from submitting, highlight the textbox, etc). How would I do this from code?

@Html.LabelFor(m => Model.Slug)
@Html.TextBoxFor(m => Model.Slug)
<span id="UrlMsg" class="field-validation-error" style="display: none;"></span>

 if (error) {
        $('#UrlMsg').html('This name is already in use.').fadeIn('fast');
        //what should I do here for the rest of the validation?
 }

推荐答案

首先,您可以添加验证摘要:

First, you can add a validation summary:

@Html.ValidationMessageFor(m => m.Slug)

然后您可以使用 .showError 方法手动触发 jQuery 验证/非侵入式验证.这是一个示例:

Then you can manually trigger jQuery validation / unobtrusive validation with the .showError method. Here is a sample:

var errorArray = {};
errorArray["Slug"] = 'Some error message for the Slug text box';
$('#SomeFormId').validate().showErrors(errorArray);

这篇关于在文本框上手动设置不显眼的验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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