本文介绍了基于正则前pression属性客户端的正则表达式验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模型我指定的正规的前pression简单的密码验证

In my model I specified a regular expression for simple password validation

[Required(ErrorMessage="Error")]
[RegularExpression("(?=.{6,})(?=.*\\d)|(?=.*\\W)", ErrorMessage= "Error")]
public string Password { get; set; }

由于预计产生的HTML元素

As expected this produces an html element

<input class="textbox-regular" data-val="true" data-val-regex="Error" data-val-regex-pattern="(?=.{6,})(?=.*\d)|(?=.*\W)" data-val-required="Error" id="Password" name="Password" type="password" />

这是一个有效的JavaScript正则表达式,但密码永不相匹配。这是JQuery的验证程序的限制?

This is a valid JavaScript Regex but password never matches. Is this a limitation of JQuery validator?

THX

推荐答案

虽然有单个正前pressions,会做什么,我想,我能实现打破这一成3验证更好的用户体验,所以用户获得关于需要什么特定消息。例如:

Although there are single regular expressions that will do what I wanted, I can achieve a better user experience by breaking this up into 3 validations, so that the user gets specific messages about what is needed. for example

[Required(ErrorMessage="Please enter a password")]
[StringLength(100, MinimumLength = 6, ErrorMessage = "Password must be at least 6 characters long")]
[RegularExpression("(.*\\d.*)|(.*[@#$%^&+=\\*].*)", ErrorMessage = "Password must contain a digit or special character")]

这篇关于基于正则前pression属性客户端的正则表达式验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:16