Expression regular for password

Viewed 39

I need the correct regular expression pattern, for checking input text in HTML matching this format:

  • Start with a capital letter

  • End with the character $

  • Between the initial capital letter and the final dollar character there may be:

    1. Letter between a and z (both included) uppercase or lowercase
    2. Digit from zero to nine (two included)
    3. The underscore character ( _ )

Example: B4rc3l0nA$, Fr4_nc3$, A$

I think I have to add anchors at the beginning and at the end, but I don't know how to join everything so that the complete regular expression remains.

<input type="text" placeholder="Your password" pattern="^\[a-z]i\s-\s[0-9]{2}\([a-z]{1,3})$" required>
1 Answers

You can use this regular expression.

^[A-Z]\w+?\$$
  1. ^[A-Z] meaning start with capital character as first letter.
  2. \w+? any letter or number or underscore
  3. \$$ dollar sign at the end.

const checkPassword = (password) => {
  return /^[A-Z]\w+?\$$/.test(password)
}


console.log(checkPassword("B4rc3l0nA$"))
console.log(checkPassword("Fr4_nc3$"))
console.log(checkPassword("Fr4_nc3"))
console.log(checkPassword("fr4_nc3$"))
console.log(checkPassword("Ab!!@@ÅÆØ}$"))

FOR TESTING

Related