Nemoden
Nemoden

Reputation: 9056

jquery custom filter does not work

The filter itself is pretty easy:

$.expr[':'].valid = function(a) {
    var phone = a.value.replace(/\D/g,''),
        phonesub = phone.substr(0, 2);
    return (phonesub == '79' || phonesub == '89') && phone.length == 11 
}

I just want to check if phone length us 11 and it starts with 79 or 89. However:

$(":valid")
[<input type=​"text" id=​"phone1" name=​"phone1" value>​]

it's clear that value is "".

Why my filter doesn't work?

BTW, at the same time, there is another input on the DOM:

<input type="text" id="phone2" name="phone2" value="" disabled="">

and it's not getting matched by the filter. Is it affected by disabled?

Let's change it's value:

$("#phone2")
[<input type=​"text" id=​"phone2" name=​"phone2" value disabled>​]
$("#phone2").val("79111111111")
[<input type=​"text" id=​"phone2" name=​"phone2" value disabled>​]
$("#phone2").val()
"79111111111"
$(":valid")
[<input type=​"text" id=​"phone1" name=​"phone1" value>​]

I'm desperate

Upvotes: 2

Views: 101

Answers (2)

fliptheweb
fliptheweb

Reputation: 1166

Because "valid" its reserved filter name in jQuery. Use "validPhone" or other name instead of "valid".

Upvotes: 2

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123367

Since a is a dom element you should write

a.value.replace(/\D/g,''),

or

$(a).val().replace(/\D/g,''),

Upvotes: 0

Related Questions