+ 1
Password Validation(Javascript)
How can I check if password starts with an alphabet?
8 ответов
+ 4
For example with Regular Expressions:
var password = "My3Password_";
var patt = /\b[a-z]/i;
var res = patt.test(password);
// res = true
+ 3
For example:
string.charAt(i) and i = 0 where string is the password,
that means:
string.charAt(0)
+ 3
Ja Play In your regex example, you have an extra unwanted escape character \ after the b in your patt string.
+ 3
Ja Play You say it worked but, for me, it gave False as an output. 🤔
+ 2
Ja Play Also, not wanting to pick holes in your solution, but
var password = "3My3Password_@a";
var patt = /\b[a-z]/i;
also returns true. You can simply use an anchor to search only the start of the string.
var patt = /^[a-z]/i
+ 1
In the beginning I tested with \w and then replaced with [a-z] and forgot to remove \ - but it worked.
Thanks Russ - already removed.
+ 1
Russ there isn't just one way of doing it.
+ 1
Ja Play I completely agree and I'm sorry if I upset you - that really wasn't my intention. The reason for my last comment was that I wanted to let OP be aware of a solution that I felt was reliable. I felt that your code didn't necessarily return the correct answer, and my comment contained an example where your code returned true for starting with a letter, when it wasn't the case.
I also feel that I did pick my words carefully, by saying "You can simply use an anchor..." rather than "You need to..." or "You should...". I hoped that that wording conveyed the idea that this was just an option, not the only way.
Again, I'm really sorry if I upset you.