jQuery form validate not working correctly [closed]

家住魔仙堡 提交于 2019-12-14 02:26:50

问题


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;
});

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

This is my html form

<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>


回答1:


Your button...

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

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

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

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

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

$(document).ready(function () {

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

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

});

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



来源:https://stackoverflow.com/questions/23113636/jquery-form-validate-not-working-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!