+ 2
Special characters
Please help me to detect special characters (in javascript) ? https://code.sololearn.com/W7WA40P79gxO/?ref=app
10 Answers
+ 2
The first thing is to define what you're talking by "special" char: all non-alphanumeric chars ? included punctuation or not ?...
When you know that, you can define range(s) of contiguous char (valid or invalid -- use the less range to make).
So, assuming you only want to differenciate alphanum from others, a-z are contiguous (in ascii or unicode there are equivalent), A-Z also and 0-9 too (but not each with another).
Well now, for each char you would test if it's (or it isn't) in (or outside) the ranges...
function is_alphanum(char) {
if (char.length!=1) throw Error();
var az = 'az', AZ = 'AZ', nb = '09';
return (
(az[0]<=char && char<=az[1])
|| (AZ[0]<=char && char<=AZ[1])
|| (nb[0]<=char && nb<=nb[1])
);
}
var x = is_alphanum('!');
// assign false to x
// result of is_special_char should be not x in this implementation
However, that's for learning purposes: this could be done more quickly and efficiently with regular expressions, but that's another topic :P
+ 2
Russ you're right: I was misreading your first code suggestion, and believed that you proposed to check only one char, not the entire string ;)
+ 1
A simple way would be to use includes() method.
pas.includes("!") returns true if "!" is in pas. You can only test one special character at a time though.
+ 1
goodyeea I've just spotted your updated code. Remember I said that you can only check one character at a time? đ
My idea for this was for you to set up a for loop and then cover each character in turn:
for (char of "special_chars") {
if (pas.includes(char) {
...
}
}
+ 1
Russ
... or almost conversely:
var forbiden_chars = "â©Â„ÂŁâŹ";
function has_forbiden_char(text) {
for (c of text)
if (forbiden_chars.indexOf(c) != -1)
return true;
return false;
}
Notice how we could test many special chars at once with the help of the indexOf() method (available on strings and arrays) ;)
+ 1
visph
I may be a little slow, but I'm not seeing the difference between
if (forbidden_chars.indexOf(c) == -1)
and
if (forbidden_chars.includes(c)).
In fact, looking at it again, did you not mean != -1? If c is not a forbidden character, you don't want it returning true do you? Maybe I've misunderstood.
Also, I'm not sure I understand how to use this method to check if a special character does appear in the password. Alphanumeric characters are not forbidden, so you will have to check that a character is not forbidden and that also is not alphanumeric.
I guess it comes back to what is considered a special character, but even a very long list of acceptable special characters is going to be much shorter than listing all the forbidden ones isn't it?
+ 1
Yes, the == was a typo (now corrected), obviously.
And yes also: if there are many more forbidden chars than allowed, it's more efficient to test if a char is not in allowed list than if it is in forbidden list (that was my advice in my first post, wich suggest a even more efficient method by defining ranges of chars instead of list)...
And about my last post, the proposed function test for a whole string (the loop) containing any forbidden char (each char of the input test do only one test to determine if it is in the special chars list).
+ 1
I agree about the efficiency of your method of checking for alphanumeric characters. Though I'm still not sold on your other efficiency argument.
for (char of "stringA") {
if ("stringymcstringfaceB".indexOf(char) != -1) {
...
}
}
and
for (char of "stringymcstringfaceB") {
if ("stringA".indexOf(char) != -1) {
...
}
}
...will each have the same efficiency won't they, because they do the same number of checks?
As far as I can tell, your argument is that, because the whole password is shorter than the (supposed) list of acceptable special characters, it's more efficient to loop through the shorter string, and check if each of those characters is in the longer string, right? Sorry if this is wrong.
+ 1
Ah ok, glad we got that sorted!
For what it's worth, my first suggestion was simply a hint for OP to use to try and figure the solution out themself, not a solution in itself.