简体   繁体   中英

Check string against array of strings (password validation)?

I've been googling around but can't seem to find this answer anywhere, weirdly. I need to check a single string against an array of strings, to see if any of the array strings are contained by the original string.

So, I need to check if the password contains any of the strings, not if it is equal.

Eg:

var password = 'word2'
var arrayOfStrings = ['word', 'another', 'third']

if (password.indexOf(arrayOfStrings) >= 0) {
  // valid
} else {
  // not valid
}

Wherein password is not valid.

I assume you try to check if your password contains any word of the array. Just loop it :)

for (var i=0, len=arrayOfStrings; i < len; i++) {
   if (password.indexOf(arrayOfStrings[i]) !=-1) {
      break;
      // EQUALS!!
   } else {
      // NOT EQUALS!!
   }
}

Since ES6, the newest vestion of JavaScript, you can use string.include(string) . But I have no experience with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM