jquery masked input plugin to not clear field?

Senthil Sivaramakrishnan picture Senthil Sivaramakrishnan · Aug 20, 2012 · Viewed 7k times · Source

In my project I'm using these following plugins for validation

     jquery.validate.min.js, 
     jquery.ui.core.js,
     jquery.ui.widget.js, 
     jquery.ui.mask.js

I'm calling it like this:

     $('#txtShippingInfoPhone').mask({mask:"999-999-9999",clearEmpty: false});

By default input box is empty. like this

  Phone number: 

when i keyup on that textbox its look like this

Phone number:___ ___ ____

And, If I enter wrong phone number then it is accepting. e.g:

 [407-555-____]

But here, error should through instead of accept the value. Observation: Its consider __ __ ____ also value.

Answer

gmo picture gmo · Aug 21, 2012

I don't know how you validate the form, but if is the default proceedment, you are only checking if the field is not empty (required).

You need to check if the phone value match the mask before... and then continue with the validation.

Example

if($('#txtShippingInfoPhone').mask("valid")){
    /* It's valid, continue with the submit... */
    return true;
}else{
    /* It's NOT valid, trigger the error msg, and interrupt the submit... */
    return false;
}​