Quantcast
Channel: Marius' Blog » JavaScript jQuery
Viewing all articles
Browse latest Browse all 4

How to validate a string characters with JavaScript

$
0
0

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 all articles
Browse latest Browse all 4

Trending Articles