Wasiim
Wasiim

Reputation: 146

How to remove a string from an array - Jquery

I have the following code:

var isvalid = [];
function namevalidation(){
    var checkname = $("#first-name").val();
    var namevalid = new RegExp("(^[a-zA-Z'-]+$)");
    var name_error = $("error");

    if(checkname.match(namevalid)){
        $(name_error).css("display","block");
            if($.inArray('name', isvalid) == -1){
                isvalid.push('name');
            }   
    }else{
        $(name_error).css("display","none");
            if($.inArray('name', isvalid) > -1){             
                isvalid.splice("name",1);                 
            }
    }
}

I do not know the position of the string inside the array, as i would have many similar functions for other inputs. Based on the code above how do i remove the string 'name' from the array 'isvalid'?

Upvotes: 0

Views: 39

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

.splice() takes the index of the item as the first argument, so try

var index = $.inArray('name', isvalid);
if (index > -1) {
    isvalid.splice(index, 1);
}

Upvotes: 1

Related Questions