'Adding and removing class rules in jQuery Validate

how we use addclassrules and removeclassrules in jquery plugin . with an examples.

$('#form').validate({
});

if ($(this).attr("san") != "") {
    jQuery.validator.addClassRules("san" , { required: true });
    $.validator.addMethod("san", function() {
        required: true
    }, "Please enter your name");
}
else if ($(this).attr("san") == "") {
    jQuery.validator.removeClassRules("san"); 
}


Solution 1:[1]

Your code makes very little sense as you've posted it...

if ($(this).attr("san") != "") {
    jQuery.validator.addClassRules("san" , { required: true });
    $.validator.addMethod("san", function() {
        required: true
    }, "Please enter your name");
}
else if ($(this).attr("san") == "") {
    jQuery.validator.removeClassRules("san"); 
}
  • $(this) does not represent anything in the context of your code.

  • There is no such thing as removeClassRules() in the jQuery Validate plugin.

  • You're using the .addMethod() method improperly. This method is for creating new rules (methods) from scratch. You must only put valid jQuery/JavaScript inside the function(){}. required: true; inside of a function(){} is totally meaningless, and a syntax error.

  • You're assigning the required rule to a class called san using addClassRules(). This seems unnecessary. The whole purpose of the addClassRules() method is to combine multiple standard rules into one (called a compound rule) and assign that compound rule to a class. Then the rules can be applied to input elements simply by using the class.

  • You're trying to add a method called san, create a class rule called san, while you have an attribute called san. Again, none of this makes any sense.


Quote OP:

"how we use addclassrules and removeclassrules in jquery plugin . with an examples."

You start by reading the documentation and looking at the posted examples. As you can see, there is no such thing as a removeclassrules method.

Then when asking a question here, you would clearly describe what you want it to do, what's going wrong, and include enough code for a self-contained example, including the relevant HTML markup.

See: How to create a Minimal, Complete, and Verifiable example

Solution 2:[2]

You just add required set to false in elseif condition.

if ($(this).attr("san") != "") {
    jQuery.validator.addClassRules("san" , { required: true });
    $.validator.addMethod("san", function() {
        required: true
    }, "Please enter your name");
} else if ($(this).attr("san") == "") {
    jQuery.validator.addClassRules("san" , { required: false }); 
}

Solution 3:[3]

You can do validation like this also jquery:

function test()
{ 
if ($("#txtText").val().trim() == "") {
                $("#txtText").focus();
                ValidateMessage("Please enter title.");
                return false;
            }
}
  function ValidateMessage(Message) {
            $("#pErrorMessage").html(Message);
            $("#dialogValidation").dialog('open');
        }

HTML

 <div id="dialogValidation" title="Mandatory message" style="display: none;">
        <p>
            <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
        </p>
        <p id="pErrorMessage">
        </p>
    </div>

Solution 4:[4]

//this code is for setting the rules

$.validator.addClassRules({ location_name:{ location_name:true, maxlength:30, alphabates:true } });

//this code is for displaying the which validation message you want.

 $.validator.addMethod(
    "location_name", //name of a virtual validator
    $.validator.methods.required,
    "Please enter the city"
    );

location_name will be your class name for which you want to give validation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2
Solution 3 Ashok Dhakhada
Solution 4 Premchand Dokku