Quantcast
Viewing latest article 2
Browse Latest Browse All 4

How to validate a string characters with JavaScript

This JavaScript function returns true if the characters from a string are valid, false otherwise. The characters for validation are given as a string parameter.

JavaScript code:

validateCharacters("Validate this", "abc");

function validateCharacters(str, allowedCharacters){
    var characters = str.split("");
   
    for (var i=0; i<characters.length; i++){
        if (allowedCharacters.indexOf(characters[i]) == -1){
            return false;
        }
    }
    return true;
}


Viewing latest article 2
Browse Latest Browse All 4

Trending Articles