简体   繁体   中英

regular expression for validating password field

I am a learner in java script currently I am going through the regular expression for validating the password field, I want my password field to contain characters and only one number in any place of the string,

I tried the following regular expression it checks for at-least one number and one character in the string

^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$

Can anyone help me!

All you need is adding a lookahead at the start:

^(?=\D*\d\D*$)[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$

The lookahead makes sure there is zero or more nondigits followed by a digit and zero or more nondigits up to the end of string.

Use this RE

(?!^[0-9] $)(?!^[a-zA-Z] $)^([a-zA-Z0-9]{6,15})$

A simpler expression would be

^\D*\d\D*$

where the meaning is

  • Zero or more non-numbers
  • A number
  • Zero or more non-numbers

If you want at least another character...

^(\D+\d\D*)|(\D*\d\D+)$

where the two provided alternatives require either before or after at least one non-digit.

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