-1

I am practicing regular expressions and I need a regular expression to force the user to write at least one number and at least one letter when writing the password. How can I do it? I'm using this:

'^[A-Za-z0-9ñÑáéíóúÁÉÍÓÚ ]+$'

and it does not work. this pattern is not fulfilled, I only introduce numbers and it does not require to have at least one letter.

In my real code I use:

 Validators.pattern('^[A-Za-z0-9ñÑáéíóúÁÉÍÓÚ ]+$')

For example I need validate the string

123456789a

And not works

yavg
  • 2,761
  • 7
  • 45
  • 115
  • 2
    Possible duplicate: https://stackoverflow.com/questions/1051989/regex-for-alphanumeric-but-at-least-one-character – full-stack Aug 24 '18 at 15:11
  • Use two regular expressions: `/[a-z]/i.test(password) && /\d/.test(password)` should enforce at least one letter and one number. – Patrick Roberts Aug 24 '18 at 15:12
  • @PatrickRoberts thanks.. that not works for me.. In mi case I use "123456789a' and not works.. – yavg Aug 24 '18 at 15:27
  • @yavg works fine for me: `var password = '123456789a'; /[a-z]/i.test(password) && /\d/.test(password) // returns true` – Patrick Roberts Aug 24 '18 at 15:32
  • 2
    Possible duplicate of [Regex pattern to match at least 1 number and 1 character in a string](https://stackoverflow.com/questions/7684815/regex-pattern-to-match-at-least-1-number-and-1-character-in-a-string) – JoelDoryoku Aug 24 '18 at 15:35
  • @PatrickRoberts apologize that message was not for you, in my real code I do not think you can put 2 conditions as you have it. – yavg Aug 24 '18 at 15:43

1 Answers1

0

Try this: /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/

I hope it's helps ;)

JoelDoryoku
  • 364
  • 3
  • 11
  • it returns true to me: var expression = /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/; console.log(expression.test("123456789a")); – JoelDoryoku Aug 24 '18 at 15:43