+ 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
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,}.
+ 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'
+ 2
thank u very much guys thats was helpfulđ
- 9
ayy baby you hot af