jQuery表单验证无法正常工作 [英] jQuery form validate not working correctly

查看:88
本文介绍了jQuery表单验证无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我想通过jquery.validate验证我的表单输入.这是我的剧本

My problem is i want to validate my form inputs trough jquery.validate. this is my script

$(document).ready(function () {

$('#formreg').validate({ 
    rules: {
        Username: {
            required: true,
            email: true
        },
        Password: {
            required: true,
            minlength: 8
        },
        Password2:{
            required:true,
            minlength:8
        },
        Name:{
            required:true   
        },
        Lastname:{
            required:true
        },
        Address:{
            required:true
        },
        Telephone:{
            required:true
        }
    }
});
return false;
});

使用上面的代码,我得到的唯一验证错误仅针对电子邮件,password2和地址,我想获取所有表单输入的验证错误.谢谢

with the above code the only validation errors that i get back are only for the email,password2 and address, I want to get the validation errors for all the form inputs. Thank you

这是我的html表单

<form action="#" method="post" accept-charset="utf-8" id="formreg">    
<input type="text" name="Username" value="" id="Username">
<input type="password" name="Password" value="" id="Password" />
<input type="password" name="Password2" value="" id="Password2"/>  
<input type="text" name="Name" value="" id="Name"/>
<input type="text" name="Lastname" value="" id="Lastname" />
<input type="text" name="Address" value="" id="Address"  />
<input type="text" name="Telephone" value="" id="Telephone"  />    
<button name="Login" type="button"  onclick="submitform();">Register</button>

推荐答案

您的按钮...

<button name="Login" type="button"  onclick="submitform();">Register</button>

您不需要在jQuery中使用任何内联JavaScript.而且您当然不需要jQuery Validate插件的onclick处理程序.

You do not need any inline JavaScript with jQuery. And you certainly don't need an onclick handler for the jQuery Validate plugin.

将您的type更改为submit并删除嵌入式点击处理程序...

Change your type into submit and remove the inline click handler...

<button name="Login" type="submit">Register</button>


您也不能将return false放入DOM就绪处理程序中...


You also cannot put a return false into your DOM ready handler...

$(document).ready(function () {

    $('#formreg').validate({
        // your rules
    });

    // return false;  // <- REMOVE THIS LINE

});


工作示例: http://jsfiddle.net/M9Y6L/1/


Working DEMO: http://jsfiddle.net/M9Y6L/1/

这篇关于jQuery表单验证无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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