您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何使用jQuery的validate.js表單驗證插件,代碼簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
效果:
代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>表單驗證插件Validate</title> <script src="js/jquery-1.10.2.min.js"></script> <script src="js/jquery.validate.min.js"></script> <style> body { background-color: #000; } form { width: 361px; margin: 80px auto; padding: 50px; border: 2px solid #666; box-shadow: 0 0 5px rgba(255, 255, 255, 0.2); background-color: #999; border-radius: 10px; box-sizing: border-box; } form>p { margin-bottom: 20px; color: #fff; } form>p>label { display: inline-block; width: 80px; text-align: center; } label.error { display: block; width: 100%; color: rgb(189, 42, 42); font-size: 12px; text-align: right; margin-top: 5px; } input { width: 170px; height: 20px; outline: none; background-color: #ddd; border: 1px solid #ddd; border-radius: 4px; } .submit { width: 170px; margin: 30px auto 0; } .submit input { background-color: #0099aa; color: #fff; border: 0; padding: 5px; height: 30px; } </style> </head> <body> <form id="signupForm" action="" method="post"> <p> <label for="name">姓名:</label> <input type="text" id="name" name="name"> </p> <p> <label for="email">郵箱:</label> <input type="email" id="email" name="email"> </p> <p> <label for="password">密碼:</label> <input type="password" id="password" name="password"> </p> <p> <label for="confirm_password">確認密碼:</label> <input type="password" id="confirm_password" name="confirm_password"> </p> <p class="submit"> <input type="submit" value="提交"> </p> </form> </body> <script> $(function() { $("#signupForm").validate({ rules: { name: "required", email: { required: true, email: true }, password: { required: true, minlength: 5 }, confirm_password: { required: true, minlength: 5, equalTo: "#password" } }, messages: { name: "請輸入姓名", email: { required: "請輸入Email地址", email: "請輸入正確的Email地址" }, password: { required: "請輸入密碼", minlength: "密碼不能小于5個字符" }, confirm_password: { required: "請輸入確認密碼", minlength: "確認密碼不能小于5個字符", equalTo: "兩次輸入的密碼不一致" } } }); }) </script> </html>
上述內容就是如何使用jQuery的validate.js表單驗證插件,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。