What regular expression should i use to let the password contain numbers and letters?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What regular expression should i use to let the password contain numbers and letters??

when i'm doing this /^[A-Za-z]+[0-9]$/ it takes letter fist and the rest of the expression numbers

20th Jan 2017, 4:36 PM
Mima_nf
Mima_nf - avatar
4 Answers
+ 6
/^[a-z\d]+$/i Never use 0-9, use \d it's shorter. \d is equivalent to 0-9. You only use ranges on numbers if you want to include non-zero numbers like [1-9]. Also, don't use A-Za-z, you can use a-z and make the regex case insensitive with "i". visph also included some important uses of the curly brackets on regex. Passwords are mainly 8 or more characters long so instead of using "+", you can use {8,}.
20th Jan 2017, 5:00 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 6
Yes, you should do: /^[A-Za-z0-9]+$/ So you have only one regex 'class' characters, and each ( the '+' symbol just after is for 'one of these character one or many times' ) can be letter or digit... Else, with yours, it say: 'one or many letters followed by only one digit'. Bonus: If you want to let the password be constraint to a minimal character count, you can do it by replacing the '+' by '{n,}' where 'n' is the number of minimal length. So, for five characters you do: /^[A-Za-z0-9]{5,}$/ Variations are ( with positive integers ): {n} -> exactly number of motif {n,} -> at least number of motif, could be more {n,m} -> greater than 'n', lower than 'm'
20th Jan 2017, 5:16 PM
visph
visph - avatar
+ 2
thank u very much guys thats was helpful😊
21st Jan 2017, 11:19 AM
Mima_nf
Mima_nf - avatar
- 9
ayy baby you hot af
20th Jan 2017, 4:38 PM
Donavan