用不显眼的验证处理占位符 [英] Handling placeholders using unobtrusive validation
问题描述
我有了对输入字段占位符的形式。它使用HTML5占位符属性和JavaScript占位符回退。
我用不显眼的验证,像这样:
[DisplayName的()]
[必需(的ErrorMessage =请输入名称)]
[StringLength(maximumLength:20,MinimumLength = 1的ErrorMessage =最大长度为20个字符。)
公共字符串用户名{获得;组; } [显示名称()]
[必需(的ErrorMessage =请输入注释。)
[StringLength(maximumLength:350,MinimumLength = 1的ErrorMessage =最大长度为350个字符。)
公共字符串CommentText {搞定;组; }
问题是,使用IE的占位符算作在输入字段中的字符,所以没有确认并进入没事的时候可以提交表单。
有没有一些方法,我可以添加一个条件,限制指定的字符串的用户名的注释。例如,请在此输入用户名。
我想避免编写自定义属性验证,如果这是可能的。
先谢谢了。
编辑。
使用常规的前pressions解决:
[RegularEx pression(^((?!My_Placeholder_Text)| \\ N)* $的ErrorMessage =请输入文字)]
如何使用正则表达式的属性,以确保您所查找的字符串不是提交的字符串吗?
下面的问题有实现这一目标前pressions的例子:
正则表达式来排除特定的字符串常量
I have a form that has placeholders for input fields. It uses html5 placeholder attribute and javascript placeholder fallback. I use unobtrusive validation like so:
[DisplayName("")]
[Required(ErrorMessage = "Please enter a name")]
[StringLength(maximumLength: 20, MinimumLength = 1, ErrorMessage = "max length is 20 chars.")]
public string UserName { get; set; }
[DisplayName("")]
[Required(ErrorMessage = "Please enter a comment.")]
[StringLength(maximumLength: 350, MinimumLength = 1, ErrorMessage = "max length is 350 chars.")]
public string CommentText { get; set; }
The problem is that when using IE the placeholders are counted as characters in the input fields so there is no validation and the form can be submitted when nothing is entered.
Is there some way I can add a condition to the UserName annotations that restricts a specified string. For example "Enter username here.."
I would like to avoid writing a custom property validator if this is possible.
Thanks in advance.
EDIT. Solved using regular expressions:
[RegularExpression("^((?!My_Placeholder_Text).|\n)*$", ErrorMessage = "Please enter text")]
How about using a regex attribute to make sure the string you are looking for is not in the submitted string? The following question has examples of expressions that accomplish that: RegEx to exclude a specific string constant
这篇关于用不显眼的验证处理占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!