3

I am retrieving a form with ajax, once i have received the form and added it to the DOM, i try to append some validation to it with jQuery, the result is not as expected.

The HTML that i insert into the page from an AJAX call is:

<form id="accept-form">
    <input id="txName" type="text" value="" name="txName" data-val-required="Please Select A Valid User" data-val="true">
    <span class="field-validation-valid" data-valmsg-for="txName" data-valmsg-replace="true"></span>
</form>

I then append this form to my page and attempt to create a jQuery validator for it as follows:

$("#accept-form").validate({
    rules: {
        txName: {
            required: true
        }
    },
    messages: {
        txName: {
            required: "Enter your name"
        }
    }
});

Then when i press the submit button, i have an alert set that returns True regardless of if the txName field is empty or not:

alert($("#accept-form").valid());

Have tried several different approaches, would appreciate any ideas as to what I could be doing wrong.

2 Answers 2

4

the anwser is just the way i was parsing the html with the validator.

var form = $("#accept-form");

form.unbind();
form.data("validator", null);
$.validator.unobtrusive.parse(form);
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure what unbind and data method were called for. Just calling unobtrusive.parse works fine for me.
1

put javascript code in a file and link the file to your main html page on ajax success.

if you link it to your page before ajax completion nothing would happen.

1 Comment

The jquery validation is already in the parent page. It is working to an extent but returns an incorrect result.

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.