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)

Marluxiaz
  • 29
  • 1
  • 5
  • 4
    Checkout JS [regular expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions). –  Jan 05 '17 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 Jan 05 '17 at 20:06
  • you could use a string with allowed charaters. – Nina Scholz Jan 05 '17 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 Jan 05 '17 at 20:08
  • 2
    Possible duplicate of [Javascript. Checking if string contains text from an array of substrings](http://stackoverflow.com/questions/5582574/javascript-checking-if-string-contains-text-from-an-array-of-substrings) – Heretic Monkey Jan 05 '17 at 20:08
  • HTML5 introduced [data form validation](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Data_form_validation) where you can use regex without javascript. It also adds css psudo elements if it's invalid so you can invalid fields with a stylesheet –  Jan 05 '17 at 20:08

3 Answers3

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-]+$/
jmar777
  • 38,796
  • 11
  • 66
  • 64
2

How about:

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

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

benbotto
  • 2,291
  • 1
  • 20
  • 32
  • This will fail if it encounters a `-` character, which was included in the OP's "allowed" list. – jmar777 Jan 05 '17 at 20:11
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 = " ✔";
}
motanelu
  • 3,945
  • 1
  • 14
  • 21
  • 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)) {` – Marluxiaz Jan 05 '17 at 20:13