简体   繁体   English

验证在java脚本中

[英]Validation In java script

Currently I'm attempting to create a regular expression to validate an user input field which requires the input to contain at least one of each of the following: 目前我正在尝试创建一个正则表达式来验证用户输入字段,该字段要求输入至少包含以下各项之一:

  • an upper-case character 一个大写字符
  • a digit 一个数字
  • one of the following special characters: & @ ! # + 以下特殊字符之一: & @ ! # + & @ ! # +

I attempted to create a regular expression that will validate the input correctly: 我试图创建一个正确的表达式来正确验证输入:

/^([a-zA-Z+]+[0-9+]+[&@!#+]+)$/i.test(userpass);

However, the input is not validated correctly. 但是,输入未正确验证。 How should I go about doing that? 我应该怎么做呢?

尝试这个:

/[a-z]/i.test(userpass) && /[0-9]/.test(userpass) && /[&@!#+]/.test(userpass)

you can simplify the regex with lookahead 你可以用lookahead简化正则表达式

 ^(?=.*[&@!#+])(?=.*[A-Z])(?=.*\d).+$
  ------------- --------- -------
      |        |         |->match only if there's a digit ahead
      |        |->match only if there's a uppercase alphabet ahead
      |->match only if there's any 1 of [&@!#+]

[AZ] would match a single uppercase letter [AZ]将匹配单个大写字母

\\d would match a single digit \\d将匹配一个数字


OR use search 或使用搜索

if(input.search(/\d/)!=-1 && input.search(/[&@!#+]/)!=-1) && input.search(/[A-Z]/)!=-1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM