开发者

jQuery Validation always returns true

I have this html;

<form action="" method="post" name="ApplyForm" id="ApplyForm" enctype="multipart/form-data">
  <input type=text class="required"/>
  <input type="submit" onclick="return validateForm()" class="submit" value="Submit Application" title="Submit application" />

and my script;

    $(function () {
        $("#ApplyForm").validate();
    });

    function validateForm() {
        alert($(".email").valid());

        return false;
    }

So what's happening is if I alert out alert($("#ApplyForm").valid()); I always get true yet if I alert out the individual fields I get the expected results but the form still posts back;

this code works but I am unsure why the validate is not working on the form;

    function validateForm() {
       开发者_运维百科 if ($(".required").valid() == 0)
            return false;
        else
            return true;
    }

I know this isn't much to go on but I'm hoping someone else has had this experiance and can tell me a solution.


<form action="" method="post" name="ApplyForm" id="ApplyForm" onSubmit="return validateForm()" enctype="multipart/form-data">
    <input type=text class="required"/>
    <input type="submit" class="submit" value="Submit Application" title="Submit application" />


Try adding a name attribute to your first validated element within the form:

<input type=text required name='blah' />

I spent 2 hours tracking down this same issue, and I found that if the FIRST validated element in the form has a name attribute then everything works as you would expect (that is, .valid() will return false if the form is invalid). It seems to make no difference whether the other validated elements have name attributes or not.

In normal forms, you definitely need name attributes because that's what's used when the data gets submitted to the server. But in more modern environments, such as those using Knockout, there's no reason to have a name attribute on input elements, because the data-binding works to keep your data model updated.


You should add

<input type="text" data-val="true" name="name" data-required="1">

For unobtrusive HTML 5-compatible attributes describe the validators to be attached to the input fields

Additional info Click here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜