jQuery Masked Input - Is it possible to recover the mask attached to the field?

Asked

Viewed 785 times

4

I’m using jQuery Masked Input Plugin with jQuery Validation Plugin. When a form field loses focus, the validation plugin shows a message, but this is not happening with the fields that are have masks, perhaps because the Blur event happens and checks that the field is not empty, in fact, the field is filled with the mask.

So I overwrote the method required from the validation plugin to check the masks too, but I have to check the mask. As I have several masks can not hard code the mask, I would have to call a method in the plugin that returns me the mask linked to the field. I did not find the plugin documentation and anyone with my problem.

Does anyone know if there is a method that returns the mask linked to the field?

  • 1

    Looking at the source code it seems that the value passed by parameter to the function mask is not stored anywhere.

  • I searched a little and found a solution to my problem that would be to use the unmask method and check if the value without the mask equals empty.

  • Ah, ok. I posted a partial response in every case. But you better answer yourself and accept your answer as sure.

  • I haven’t implemented it yet to see if it works, even because I only passed the value, now I have to pass the whole object. I’ll look at your answer, it might be an idea.

1 answer

1

Looking at the source code it seems that the value passed by parameter to the function mask is not stored anywhere.

What you can do by inspecting the code is to see if the field has a mask by accessing the attribute data called rawMaskFn:

$(seletor).data('rawMaskFn')

In my experience, this plugin is very limited in functionality.

  • What would be the date attribute? Does the plugin add this attribute to my input? And what about this rawMaskFn value? Do you use some other mask plugin?

  • 1

    @Philippegioseffi The function data() jQuery allows you to add and remove attributes. And, yes, the plugin adds this attribute within the function mask() and remove it in function unmask(). This can serve as a flag to indicate whether or not the field has a mask, but does not allow retrieval of the mask itself.

  • I could use attr to add attributes too, right? Is it worth a question to ask the differences between them? And you recommend some plugin to use masks?

  • Reading the documentation I saw that there are two date methods. I will read later calmly to see the differences between the three.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.