提问人:Matas Vaitkevicius 提问时间:11/9/2015 最后编辑:Matas Vaitkevicius 更新时间:11/12/2015 访问量:3587
单选按钮上的不显眼验证将被忽略,而它仅适用于相同格式 html 中的文本字段
Unobtrusive validation on radio buttons is ignored while it works fine for text fields in same form html only
问:
我正在测试具有不显眼验证的形式,后面没有 MVC 控制器运行,html 是手写的,仅用于测试(找出为什么它不起作用)而不是用于生产。
提交表单时,单选按钮没有任何变化(没有错误消息,例如没有任何验证),而同一表单中的文本字段会给出验证错误。
我已经扔掉了我能想到的任何东西,示例只是必需的验证器单选按钮和错误消息跨度,仅此而已,它仍然不起作用。
我一定在这里遗漏了一些明显的东西,当有人指出我的错误时,我可能会觉得自己像个白痴,但我还是不能花更多的时间在这件事上。
<form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/mvc/5.0/jquery.validate.unobtrusive.min.js"></script>
<div class="quote-form__question fl--title">
<div class="split-input--left">
<input class="tooltip form-control input " data-val="true" data-val-length="Please enter a firstname with no more than 20 characters" data-val-length-max="20" data-val-regex="Please enter First name with letters only" data-val-regex-pattern="^ *?[a-zA-Z]+[ a-zA-Z-_']*$"
data-val-required="Please enter your first name" id="CustomerFirstname" maxlength="20" name="CustomerFirstname" placeholder="Name" tabindex="" type="text" value="">
<div class="field-validation-error-container">
<span class="field-validation-valid field-validation-error" data-valmsg-for="CustomerFirstname" data-valmsg-replace="true"></span>
</div>
</div>
</div>
<div class="label-row-full-width">
<label for="q1-1" class="label--un-checked">
<span>Mr</span>
<input type="radio" class="js-check-style js-toggle-question js-show-button" data-val-required="Please select title" name="CustomerTitle" id="q1-1" data-val="Mr" value="Mr">
</label>
<label for="q1-2" class="label--un-checked">
<span>Mrs</span>
<input type="radio" class="js-check-style js-toggle-question js-show-button" data-val-required="Please select title" name="CustomerTitle" id="q1-2" data-val="Mrs" value="Mrs">
</label>
<label for="q1-3" class="label--un-checked">
<span>Miss</span>
<input type="radio" class="js-check-style js-toggle-question js-show-button" data-val-required="Please select title" name="CustomerTitle" id="q1-3" data-val="Miss" value="Miss">
</label>
<label for="q1-4" class="label--un-checked">
<span>Ms</span>
<input type="radio" class="js-check-style js-toggle-question js-show-button" data-val-required="Please select title" name="CustomerTitle" id="q1-4" data-val="Ms" value="Ms">
</label>
<span class="field-validation-valid" data-valmsg-for="CustomerTitle" data-valmsg-replace="true"></span>
</div>
<input type="submit" value="submit" />
</div>
</form>
我错过了什么?
答:
5赞
Matas Vaitkevicius
11/12/2015
#1
经过一番调查。
我必须做的 - 是在后端引入自定义验证器属性(我认为这超出了问题的范围,但值得链接)。
这就是它归结为仅前端解决方案的原因。
将单选按钮属性更改为
data-val-required
data-val-something
加载 jquery 和验证脚本后,注册适配器
<script type="text/javascript" language="javascript"> $.validator.unobtrusive.adapters.addBool("something", "required"); </script>
下面是工作示例:
<form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/mvc/5.0/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript" language="javascript">
$.validator.unobtrusive.adapters.addBool("mandatory", "required");
</script>
<div class="label-row-full-width">
<label for="q1-1" class="label--un-checked">
<span>Mr</span>
<input type="radio" class="js-check-style js-toggle-question js-show-button valid" data-val-mandatory="Please select title" name="CustomerTitle" id="q1-1" data-val="true" value="Mr">
</label>
<label for="q1-2" class="label--un-checked">
<span>Mrs</span>
<input type="radio" class="js-check-style js-toggle-question js-show-button valid" data-val-mandatory="Please select title" name="CustomerTitle" id="q1-2" data-val="true" value="Mrs">
</label>
<label for="q1-3" class="label--checked">
<span>Miss</span>
<input type="radio" class="js-check-style js-toggle-question js-show-button valid" data-val-mandatory="Please select title" name="CustomerTitle" id="q1-3" data-val="true" value="Miss">
</label>
<label for="q1-4" class="label--un-checked">
<span>Ms</span>
<input type="radio" class="js-check-style js-toggle-question js-show-button valid" data-val-mandatory="Please select title" name="CustomerTitle" id="q1-4" data-val="true" value="Ms">
</label>
<span class="field-validation-valid" data-valmsg-for="CustomerTitle" data-valmsg-replace="true"></span>
</div>
<input type="submit" value="submit" />
</form>
希望这能为您节省一些时间。
评论
0赞
Barak Gall
7/12/2016
很抱歉重提一个老问题,但您能解释一下为什么开箱即用的解决方案不起作用吗?
3赞
Matas Vaitkevicius
7/12/2016
据我所知,只是为了或类似愚蠢的事情。Data-val-required
input type='text'
0赞
Bob.at.Indigo.Health
11/25/2019
只是一个猜测......可能是因为单选按钮具有非 null 属性,该属性满足“必需”验证。但是,单选按钮输入实际上并不包含在 POST 请求中,除非用户选择它们。value
1赞
John
12/15/2021
我知道这就像很久以前一样,但你救了我!房子里有免费饮料!(不要从屋顶上滑下来!
0赞
Matas Vaitkevicius
12/16/2021
@John非常欢迎你,约翰。:)
评论