Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"); 
}
share|improve this question
1  
can u please format code? –  jQuery Angry Bird Jul 1 at 6:44
    
what is not working then –  jQuery Angry Bird Jul 1 at 6:46
    
in this code aaclarules working while click , but error not resole after using remove class –  user3682603 Jul 1 at 6:58
    
Where is the HTML markup? What exactly are you trying to achieve. –  Sparky Jul 1 at 14:52
    
There is no such thing as a .removeClassRules() method, so that would be one issue. –  Sparky Jul 1 at 15:09
add comment

3 Answers 3

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

share|improve this answer
    
thnx for ur guidance @sparky , I will try to make more my questions more accurate in future . –  user3682603 Jul 2 at 5:54
add comment

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>
share|improve this answer
    
i want to validate through a custom msg , not by css –  user3682603 Jul 1 at 7:00
    
ya, using this you can pass customize msg to validate function –  Ashok Dhakhada Jul 1 at 7:03
    
cutom msg working but when I enter any name , msg still appear –  user3682603 Jul 1 at 7:09
    
<script> $('#form').validate({ }); jQuery.validator.addClassRules("class_1" , {required:true}); $.validator.addMethod("class_1", function() {required:true},"Please enter your name"); </script>this is my code –  user3682603 Jul 1 at 7:10
add comment

//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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.