jQuery rules add mask

Good day,

I am support an project that develop by other team.

I saw some jQuery code that I am not really understand what the code is doing, I plan to amend on it but at first I need to know what it want to do first.

Here is the code:

    $(':input[name=country]').rules('add', {
        required: true,
        mask: /^[a-zA-Z0-9 ]+$/

I am suspecting it is mask method, for example which is:

$(':input[name=country]').mask("0000-0000"); // this is work

But I try to run it but it fail, hitting error:

Cannot read property 'call' of jquery.validate.min.js:16 undefined

enter image description here Any one know what is the code trying to do?

Answers 1

  • The .rules() method is part of the jQuery Validate plugin and has absolutely nothing to do with the .mask() method which is part of another plugin. This code is trying to dynamically add form validation rules to an input element with name="country".

    $(':input[name=country]').rules('add', {
        required: true,
        mask: /^[a-zA-Z0-9 ]+$/. // <- there is no such rule called 'mask': REMOVE THIS LINE

    The error is because jQuery Validate is looking for a rule called mask where there is no "mask" rule in this plugin.

    Your only options are:

    1. remove all references to this nonexistent rule, OR
    2. create a custom rule called mask using the .addMethod() method

Related Articles