1

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)

6
  • 4
    Checkout JS regular expressions. 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. Commented Jan 5, 2017 at 20:06
  • you could use a string with allowed charaters. 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? Commented Jan 5, 2017 at 20:08
  • 2
    Possible duplicate of Javascript. Checking if string contains text from an array of substrings Commented Jan 5, 2017 at 20:08

3 Answers 3

3

This 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-]+$/
Sign up to request clarification or add additional context in comments.

1 Comment

/^[\w-]+$/ Perfect.
2

How about:

if (username.match(/[^\w-]/) !== null) {
  console.log('username has non-word characters...');
}

See here for what \w does: MDN Regular Expression.

1 Comment

This will fail if it encounters a - character, which was included in the OP's "allowed" list.
0

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 = " ✔";
}

1 Comment

This works for me, Thank you for adding it into my code makes it so much easier to work with. I changed: if (/^[a-z0-9\-\_]+$/.test(username)) { to if (/^[a-z0-9A-Z\-\_]+$/.test(username)) {

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.