Password Validation(Javascript) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Password Validation(Javascript)

How can I check if password starts with an alphabet?

26th Jul 2020, 7:06 AM
HD372
HD372 - avatar
8 Answers
+ 4
For example with Regular Expressions: var password = "My3Password_"; var patt = /\b[a-z]/i; var res = patt.test(password); // res = true
26th Jul 2020, 7:48 AM
JaScript
JaScript - avatar
+ 3
For example: string.charAt(i) and i = 0 where string is the password, that means: string.charAt(0)
26th Jul 2020, 7:24 AM
JaScript
JaScript - avatar
+ 3
Ja Play In your regex example, you have an extra unwanted escape character \ after the b in your patt string.
26th Jul 2020, 9:09 AM
Russ
Russ - avatar
+ 3
Ja Play You say it worked but, for me, it gave False as an output. 🤔
26th Jul 2020, 9:50 AM
Russ
Russ - avatar
+ 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
26th Jul 2020, 10:00 AM
Russ
Russ - avatar
+ 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.
26th Jul 2020, 9:37 AM
JaScript
JaScript - avatar
+ 1
Russ there isn't just one way of doing it.
26th Jul 2020, 10:50 AM
JaScript
JaScript - avatar
+ 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.
26th Jul 2020, 11:00 AM
Russ
Russ - avatar