0

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 Answer 1

1

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")
Sign up to request clarification or add additional context in comments.

1 Comment

D'oh! man I hate it when I do stuff like that. It worked like a charm, thanks man!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.