如何解决在 IE 中为 cshtml 获取输入类型号中的逗号
我们在 cshtml 页面中输入了类型编号。它在表单验证时在 chrome 中工作正常。 但是在 Internet Explorer (IE) 中,它在数字字段中接受逗号 (,) 并将其与逗号一起保存。 请建议。
cshtml 代码:
@Html.TextBoxFor(m => m.xxx,null,new { @class = "form-control",@required = "required",@type = "number",@step = "0.01",@id = "xxx" })
IE 截图:
在 IE 中,我们可以输入逗号并接受它的值。
Chrome 屏幕截图:
这里不允许输入逗号。
在 IE 中我也不想要逗号。这有什么限制吗?
解决方法
您可以尝试使用表单验证。并使用如下所示的输入(不使用@Html.TextBoxFor)。我有一个模型,它有一个属性 Id,这里是一个演示:
查看:
<form id="myform" method="post">
<input name="Id" type="number" step="0.01" class = "form-control"/>
<input type="submit" value="submit" />
</form>
js:
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<script>
$(function () {
$("#myform").validate({
rules: {
Id: {
required:true,doubles: true
},},messages: {
Id: {
//doubles: "You can only enter numbers and dot"
},}
})
})
$.validator.addMethod("doubles",function (value,element,params) {
var doubles = /^[0-9]*([.][0-9]{1,2})?$/;
return this.optional(element) || (doubles.test(value));
},"You can only enter numbers and dot");
</script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。