匹配'密码'和'管理'不工作jQuery验证 [英] Jquery validation on matching 'password' and 'admin' not working

查看:174
本文介绍了匹配'密码'和'管理'不工作jQuery验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个正前$ P $口令pssion:

I have tried to write a regular expression for passwords:

public class ApplicationUser : IdentityUser, ITimeStamps
{
    public const string PasswordRegularExpression = @"admin|password";
    // public const string PasswordRegularExpression = @"/admin|password/i"; // Tried this too
    // public const string PasswordRegularExpression = @"/(admin|password)/i"; // Tried this too

这是过去,高于正常Microsoft身份的东西:

This is over and above the normal Microsoft identity stuff:

manager.PasswordValidator = new PasswordValidator
{
     RequiredLength = 6,
     RequireNonLetterOrDigit = false,
     RequireDigit = true,
     RequireLowercase = false,
     RequireUppercase = false,
};

下面是我注册的视图模型:

Here's my Register view model:

public class RegisterViewModel
{
    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    [RegularExpression( ApplicationUser.PasswordRegularExpression, ErrorMessage = "The {0} must contain atleast 1 number and must not contain the word 'admin' or 'password'" )] 
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

我想,当有人使用密码或管理员密码里面扔在jQuery验证错误。然而,jQuery验证似乎并不能按预期工作。什么可能我会丢失?

I am wanting to throw an error in jquery validation when someone uses password or admin inside their password. However, the jquery validation does not seem to be working as expected. What might I be missing?

这是asp.net生成的HTML如下:

The generated html from asp.net looks like this:

<input class="form-control input-validation-error" data-val="true" data-val-length="The Password must be at least 6 characters long." data-val-length-max="100" data-val-length-min="6" data-val-regex="The Password must contain atleast 1 number and must not contain the word 'admin' or 'password'" data-val-regex-pattern="admin|password" data-val-required="The Password field is required." id="Password" name="Password" placeholder="Enter a password" type="password">

测试用例:


  • 我加括号,它仍然无法正常工作。我在密码输入,它实际上并没有失败!后来我把另外1个字符(不要紧,它是什么),然后失败......现在我删除单词密码的一部分,所以例如pasrd21234和错误仍然存​​在!

旁注:

  • I have tried using this to test it: http://regexhero.net/tester/ and the regex seems to work fine there?
  • I have also tried using this to test it: http://regexpal.com/ and it works fine?

推荐答案

在<一个基于答案href=\"http://stackoverflow.com/questions/406230/regular-ex$p$pssion-to-match-text-that-doesnt-contain-a-word\">Regular前pression匹配文本*不*包含一个字?,下面的正前pression应匹配任何密码的除了的,如果它包含管理员或密码:

Based upon the answer at Regular expression to match text that *doesn't* contain a word?, the following regular expression should match any password except if it contains "admin" or "password":

^((?!(admin|password)).)*$

我已经为这个小提琴上 Regex101 为之前加入到这一测试你的应用程序。

I've created a fiddle for this on Regex101 for testing prior to adding this into your application.

这篇关于匹配'密码'和'管理'不工作jQuery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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