kuppu
kuppu

Reputation: 1274

how to validate the input value is present in the array with other values using jquery

This is my code. i want to check the input value with the array as prefixes and user must enter this any one array value as prefixes with its own value. js:

var per =["00162", "001187", "00188e", "002163", "002491"];
var isValid = false;
$.each(per , function(index,value) {
            var i = [index];
            console.log(i);
        });
        for(var j=0; j<i; j++) {
            if(per[j] === value) {
                isValid = true;
            }
        }

Upvotes: 0

Views: 33

Answers (1)

A. Wolff
A. Wolff

Reputation: 74420

You should test a regex instead, for example still using an array for prefixes:

var per = ["00162", "001187", "00188e", "002163", "002491"];
var reg = new RegExp("^("+per.join('|')+")")
var isValid = reg.test(userInput);

Where userInput is the value to test.

DEMO jsFiddle

Upvotes: 1

Related Questions