jQuery验证始终返回true [英] jQuery Validation always returns true

查看:90
本文介绍了jQuery验证始终返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个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" />

和我的脚本;

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

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

        return false;
    }

所以发生的事情是,如果我发出警报alert($("#ApplyForm").valid());,我总是会成真,但是如果我发出警告个别字段,则会得到预期的结果,但是表单仍会发回;

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.

推荐答案

您应该添加

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

对于不打扰的HTML 5兼容属性,请描述要附加到输入字段的验证器

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

其他信息单击此处

这篇关于jQuery验证始终返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆