j_arab
j_arab

Reputation: 35

javascript regex - test is not a function

I'm trying to validate a password and if it is ok, it will keep in an array regular expression only once. When I run the script in the mozilla browser, I get the following error: 'seguridadPass [i]. Test is not a function'. I would also like to know how I can do to delete the array 'comprobaciones', those regex that have not been used. Thank you for your help.

CODE

JAVSCRIPT

var compMay = /[A-ZÑ]/;
var compMin = /[a-zñ]/;
var compNum = /(?=.*\d)/;
var compCarEsp = /[!@#$%^&*(){}[\]<>¿¡?/|.:;_-]/;
var seguridadPass = []; 
seguridadPass.push(compMin,compMay,compNum,compCarEsp);
comprobaciones = [];

$('#write').keyup(function(){

var pass = $(this).val();
if(pass!=''){
    for(i=0;i<seguridadPass.length;i++){
        if(seguridadPass[i].test(pass)){
            //alert(seguridadPass[i]);
            for(j=0;j<comprobaciones.length;j++){
                if(comprobaciones[j]!=seguridadPass[i]){
                    comprobaciones.push(seguridadPass[i]);
                }
            }
        }
    }
}
else{
    comprobaciones.splice(0,comprobaciones.length);
    //comprobaciones = [];
}
});

Upvotes: 0

Views: 2596

Answers (2)

p.s.w.g
p.s.w.g

Reputation: 149020

Your array mostly contains RegExp objects, but you've also pushed the number 8 to your array here:

seguridadPass.push(compMin,compMay,compNum,compCarEsp,8);

The Number type in JavaScript does not have a test method*. Remove the 8 and it should work:

seguridadPass.push(compMin,compMay,compNum,compCarEsp);

* Unless you provide one in Number.prototype.

Upvotes: 0

Virus721
Virus721

Reputation: 8335

seguridadPass.push(compMin,compMay,compNum,compCarEsp,8);

8 is not a regexp.

Upvotes: 3

Related Questions