views:

63

answers:

1

Hi, I'm using a swap value script to put the field label inside the input, on focus it clears and on blur it keeps the value you entered. I need to make a validator.addMethod so it won't validate with its initial value. Here's the swap value script

$(function() {
        swapValues = [];
        $(".swap_value").each(function(i){
            swapValues[i] = $(this).val();
            $(this).focus(function(){
                if ($(this).val() == swapValues[i]) {
                    $(this).val("");
                }
            }).blur(function(){
                if ($.trim($(this).val()) == "") {
                    $(this).val(swapValues[i]);
                }
            });
        });
    });

and the validator.addMethod for a text input who's value/label is "First Name"

$.validator.addMethod(
            "Foo",
            function (value, element) {
                if ($("#firstName").val === "First Name") {
                return false;
            } else return true;
        },
        "First name required"
    );

With this, when you enter in the text box and enter a different value its fine, when you exit the text box it reverts to the initial value. Is this due to my swap value routine or the validator.addMethod? when I remove the validator.addMethod it works fine. thanks,

+1  A: 

I'm not sure what effect this will have on your problem, but in your addMethod function you have:

if ($("#firstName").val === "First Name")

but you should have:

if ($("#firstName").val() === "First Name")
Ken Redler
D'oh! man I hate it when I do stuff like that. It worked like a charm, thanks man!
Dirty Bird Design