0

I'm trying to validate a form, that has both hidden and not hidden input. I added in the form validation settings the line ignore: "" to validate the hidden input, but doing so, it doesn't validate required text input that is before it, but only when put AFTER it. Another thing that happens, is that the validation of firstname works on blur after the first submit (but still it doesn't block the submit itself)....

Here is my code, and a link to js fiddle DEMO:

JS:

    var countries = [{label:"Vanuatu", code:"VU"},
        {label:"Ecuador", code:"EC"}];    
    $().ready(function() {
$('#country').autocomplete({
    source : function(request, response) {
        var matcher = new RegExp('^'+$.ui.autocomplete.escapeRegex(request.term), "i");
        response($.grep(countries, function(element, index) {
            return matcher.test(element.label);
        }));
    },
    delay : 0,
    change : function(event, ui) {
        $('#countryCode').val(ui.item ? ui.item.code : '');
    }
});
$("#signupForm").validate({
    ignore: "",
    rules: {
        firstname: {
            required: true
        },
        lastname: {
            required: true
        },
        country: {
            required: true
        },
        countryCode: {
            required: function(element){
                return $("#signupForm").validate().element( "#country" );
            }
        },
        email: {
            required: true,
            email: true
        }
    },  
    messages: {
        firstname: "Please enter your first name",
        lastname: "Please enter your last name",
        country: "Please select a country",
        countryCode: "Please select a valid country",
        email: "Please enter a valid email address"
    },
    submitHandler: function(form) {
        alert("fine!");
    }
});

});
HTML form:

    <form id="signupForm" method="post" name="" action="">
<p>
        <label for="firstname"></label>
        <input class="inputText" type="text" id="firstname" name="firstname" placeholder="First Name">
    </p>
            <p>
        <label for="country"></label>
        <input class="inputText" type="text" id="country" name="country" placeholder="Country"/>

        <input type="hidden" name="countryCode" id="countryCode" />
    </p>
                <p>
        <label for="lastname"></label>
        <input class="inputText" type="text" id="lastname" name="lastname" placeholder="Last Name">
                    </p>
                    <p>
        <label for="email"></label>
        <input class="inputText" type="text" id="email" name="email" placeholder="Email">
                        </p>
                        <p>
        <input class="submit" type="submit" id="signupButton" value="SUBMIT" />   
                        </p>
    </form> 
4
  • what is the logic for countryCode validation? Is it required only if country is specified? Commented Feb 23, 2013 at 15:46
  • yes, since I need to validate that a valid country is selected from the autocomplete. In this way I have the validation that country is filled, and the countryCode is selected (the countryCode is needed server side). Commented Feb 23, 2013 at 15:52
  • Can you check jsfiddle.net/arunpjohny/68uu5/3 and see whether your requirement is met Commented Feb 23, 2013 at 15:54
  • Also see this answer for the proper way to use the ignore option. Should be ignore: [] See stackoverflow.com/a/8565769/594235 Commented Feb 23, 2013 at 17:22

1 Answer 1

1

I think the problem is with the countryCode validation

You can try

countryCode: {
    required: function(element){
        return $("#country").val().length > 0;
    }
}

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

great thanks a lot! I thought the .validate() on country element would have done that.... do you think it's a bug to report to the developers?
I think the element method is designed to be called with the callback methods, any way you can check with the developers

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.