I'am making a user registration page, and I don't want any char's that does not match the array.
function create(){
var allowed = [
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"1","2","3","4","5","6","7","8","9","0","_","-"];
var username = $("#username").val();
if (username == ""){
document.getElementById("usernameerror").style.color = "red";
document.getElementById("usernameerror").innerHTML = " Username cannot be blank.";
}else{
if (username.indexOf(allowed) != -1){
document.getElementById("usernameerror").style.color = "red";
document.getElementById("usernameerror").innerHTML = " No symbols.";
}else{
document.getElementById("usernameerror").style.color = "blue";
document.getElementById("usernameerror").innerHTML = " ✔";
}
}
}
I bet it's something simple.. (not sub string)
I'am making a user registration page, and I don't want any char's that does not match the array.
function create(){
var allowed = [
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"1","2","3","4","5","6","7","8","9","0","_","-"];
var username = $("#username").val();
if (username == ""){
document.getElementById("usernameerror").style.color = "red";
document.getElementById("usernameerror").innerHTML = " Username cannot be blank.";
}else{
if (username.indexOf(allowed) != -1){
document.getElementById("usernameerror").style.color = "red";
document.getElementById("usernameerror").innerHTML = " No symbols.";
}else{
document.getElementById("usernameerror").style.color = "blue";
document.getElementById("usernameerror").innerHTML = " ✔";
}
}
}
I bet it's something simple.. (not sub string)
Share Improve this question edited Jan 5, 2017 at 20:16 Marluxiaz asked Jan 5, 2017 at 20:02 MarluxiazMarluxiaz 291 silver badge5 bronze badges 6- 4 Checkout JS regular expressions. – user378704 Commented Jan 5, 2017 at 20:04
-
Probably easier to use a regex, but if you really want to do it with an array, you'd need to loop through each character in the string and check if all of them are in the array. Something like
username.every(c => allowed.indexOf(c) > -1)
if you're using ES6. – Heretic Monkey Commented Jan 5, 2017 at 20:06 - you could use a string with allowed charaters. – Nina Scholz Commented Jan 5, 2017 at 20:08
- Are you using this array? If so did you really want to restrict all these characters. Or are you testing against a preset or user created array? – andre mcgruder Commented Jan 5, 2017 at 20:08
- 2 Possible duplicate of Javascript. Checking if string contains text from an array of substrings – Heretic Monkey Commented Jan 5, 2017 at 20:08
3 Answers
Reset to default 3This is exactly the kind of problem that regular expressions are designed to solve. Try replacing this line:
if (username.indexOf(allowed) != -1){
...with this:
if (!/^[a-z0-9_-]*$/i.test(username)) {
Your requirements are very similar to the \w
metacharacter as well, which would let you alternatively use this for your regex:
/^[\w-]+$/
How about:
if (username.match(/[^\w-]/) !== null) {
console.log('username has non-word characters...');
}
See here for what \w does: MDN Regular Expression.
Check this out:
if (/^[a-z0-9\-\_]+$/.test(username)) {
document.getElementById("usernameerror").style.color = "red";
document.getElementById("usernameerror").innerHTML = " No symbols.";
}else{
document.getElementById("usernameerror").style.color = "blue";
document.getElementById("usernameerror").innerHTML = " ✔";
}