Jquery Validate 不採用submit方式驗證資料方式__Jquery

來源:互聯網
上載者:User

jquery.validate.js  自動在表單(submit ) 提交的時候進行資料驗證

$.validator.setDefaults({submitHandler: function() {alert("submitted!");}});$().ready(function() {// validate the comment form when it is submitted$("#commentForm").validate();// validate signup form on keyup and submit$("#signupForm").validate({rules: {firstname: "required",lastname: "required",username: {required: true,minlength: 2},password: {required: true,minlength: 5},confirm_password: {required: true,minlength: 5,equalTo: "#password"},email: {required: true,email: true},topic: {required: "#newsletter:checked",minlength: 2},agree: "required"},messages: {firstname: "Please enter your firstname",lastname: "Please enter your lastname",username: {required: "Please enter a username",minlength: "Your username must consist of at least 2 characters"},password: {required: "Please provide a password",minlength: "Your password must be at least 5 characters long"},confirm_password: {required: "Please provide a password",minlength: "Your password must be at least 5 characters long",equalTo: "Please enter the same password as above"},email: "Please enter a valid email address",agree: "Please accept our policy"}});// propose username by combining first- and lastname$("#username").focus(function() {var firstname = $("#firstname").val();var lastname = $("#lastname").val();if (firstname && lastname && !this.value) {this.value = firstname + "." + lastname;}});//code to hide topic selection, disable for demovar newsletter = $("#newsletter");// newsletter topics are optional, hide at firstvar inital = newsletter.is(":checked");var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");var topicInputs = topics.find("input").attr("disabled", !inital);// show when newsletter is checkednewsletter.click(function() {topics[this.checked ? "removeClass" : "addClass"]("gray");topicInputs.attr("disabled", !this.checked);});});

<form class="cmxform" id="commentForm" method="post" action="/post.php"><fieldset><legend>Please provide your name, email address (won't be published) and a comment</legend><p><label for="cname">Name (required, at least 2 characters)</label><input id="cname" name="name" minlength="2" type="text" required></p><p><label for="cemail">E-Mail (required)</label><input id="cemail" type="email" name="email" required></p><p><label for="curl">URL (optional)</label><input id="curl" type="url" name="url"></p><p><label for="ccomment">Your comment (required)</label><textarea id="ccomment" name="comment" required></textarea></p><p><input class="submit" type="submit" value="Submit"></p></fieldset></form>

jquery.validate  不採用表單的 submit 方式進行資料驗證

function JqValidate(){return $( '#formLogin' ).validate({rules:{truename:'required',password:'required',},messages:{truename:'請輸入登入使用者名稱',password:'密碼不可為空',},});}function submitBtn(){ if( JqValidate() ){   $( '#formLogin' ).submit(); }}

<form id="formLogin" class="formLo" autocomplete="off" accept-charset="UTF-8" action="" method="POST"><div id="regForm">  <div class="inputL name"><label><span>使用者名稱:</span><input id="username" class="input error" type="text" value="" name="truename" placeholder="請輸入帳號"></label>  </div><div class="btns"><a class="submit" onclick="submitBtn();" href="javascript:void(0);">登陸</a></div></div></form>


Jquery validate 文檔地址:https://github.com/DiegoLopesLima/Validate#readme




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.