MVC验证regex属性可在C#中使用,但不能在JavaScript中使用 [英] MVC validation regex attribute works in C# but not JavaScript

查看:87
本文介绍了MVC验证regex属性可在C#中使用,但不能在JavaScript中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过MVC的正则表达式属性对表单字段进行验证.但是看来,无论我使用什么正则表达式,验证都只能在服务器端进行,而不能在浏览器中进行.

我用于验证的代码是:

[DisplayName("Email Address")]
[Required]
[RegularExpression(@"^[a-zA-Z0-9\.-]*@[a-zA-Z0-9\.]*\.[a-zA-Z\.]{2,6}$", ErrorMessage = "Valid email required.")]
public string emailAddress { get; set; }

正则表达式将无法通过"asd""asd@"进行验证,但是当不应该通过"asd@asd"时它将开始通过验证.将正则表达式粘贴到 http://regexpal.com/中将显示该正则表达式仅适用于完整的电子邮件.

屏幕截图: http://puu.sh/2P05x.png

如果有帮助,则可以在Kendo UI网格编辑弹出窗口中使用.

解决方案

显然,这是我正在使用的KendoUI版本中的当前已知错误.我不确定是否有适用的更新.此处的更多信息,

The regular expression will fail validation with "asd", "asd@" but it starts to pass validation at "asd@asd" when it shouldn't. Pasting the regular expression into http://regexpal.com/ will show that it should only work with full emails.

Screenshots: http://puu.sh/2P05x.png

If it helps, this is being used in a Kendo UI grid edit pop up.

解决方案

Apparently this is a currently known bug in the version of KendoUI i was using. Im not sure if there is an applicable update for it yet. More information about it here, http://www.kendoui.com/forums/mvc/general-discussions/regular-expression-validator-not-working-in-grid-popup.aspx

这篇关于MVC验证regex属性可在C#中使用,但不能在JavaScript中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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