简体   繁体   中英

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

 ^(?=.*[&@!#+])(?=.*[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

\\d would match a single digit


OR use search

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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