tags:

views:

41

answers:

1

Possible Duplicate:
jQuery custom validation method issue

I have a select list #technology. It's default value is "0" due to back end payment processor. I need a validation rule (using jquery.validate.js) that makes it return false if it's value is "0" but return true for any other value. What I am trying isn't working.

<script type="text/javascript">
    $(document).ready(function() {
        $.validator.addMethod(
            "SelectTechnology",
            function(value, element) {
                //var selectedCountry = $('#Country').val();
                if ($("#singleTech").val() === '0'){
                    return false;
                } else return true;
            },
            "Please Select a Technology"
        );
        var validator = $("#SinglePmnt").validate({
            rules: {
                    technology: {
                        SelectTechnology: true
                    }
            },
            errorElement: "span",
            errorPlacement: function(error, element) {
                error.appendTo(element.parent("td"));
            }
        });
    });
</script>

It doesn't clear the error if you hit submit with the default value selected then change it. thanks

A: 

You should also use value to get the passed value

if (value == '0')
RememberME
The extra = is the strict equality operator, which exists in many languages, including javascript. Basically, it checks not only that the values are equal, but also that the variable types are equal. So while 0 == '0', 0 !== '0'. See here for more info: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators
treeface
Thanks. Didn't know that. I suspect the value is the real culprit. I don't know what field `$("#singleTech").val()` is referencing.
RememberME
@RememberME when I change to #("#singleTech").val() == '0' it still has the same issue. If you submit when that value is selected, it displays the error msg as expected. When you change to a different value, it doesn't remove the error msg, although it will submit
Dirty Bird Design
And here's an example of this in action: http://jsfiddle.net/BszxG/
treeface
@treface @RememberME #singleTech is a select list, the default value is "0" I don't have an issue getting it to validate, it doesn't remove the validation error msg when you change the select's value.
Dirty Bird Design
The validation is validating #technology, but checking the value of #singleTech. It will only validate when #technology is changed, not when #singleTech is changed.
RememberME
@Dirty, have you taken a look at my answer? It seems to be working fine, but I'm not entirely sure.
treeface
OK. I see from your previous post that one is the id and one is the name. You should use `value`, although I'd think your way should work.
RememberME
@treeface - saw your answer on my previous post, doesn't work outside of jsfiddle. not sure what is wrong with my code! rggghhh!
Dirty Bird Design
@RememberME - you mean in place of .val?
Dirty Bird Design
In place of `$("#singleTech").val()`. The function is being passed `value` which is the value of technology.
RememberME
@RememberMe - so how do I fix that?
Dirty Bird Design
live link http://www.kinetick.com/V3/purchase_own.php
Dirty Bird Design
In the .addMethod function replace `if ($("#singleTech").val() === '0')` with `if (value == '0')`
RememberME
@RememberMe, thanks Ill give it a try. Much appreciated. Same thing, it will validate but isn't removing the error message onchange.
Dirty Bird Design