This article mainly introduces the example code for jquery to complete form verification. The code is simple and easy to understand. Friends who need it can refer to it
No more nonsense, I will post the code directly for everyone. The specific code is as follows Shown:
<!doctype html> <head> <meta charset=utf-8" /> <title>表单验证</title> <link href="css/style1.css" rel="external nofollow" rel="stylesheet" type="text/css" /> <!-- 引入jQuery --> <script src="jquery/jquery.js" type="text/javascript"></script> <script> $(document).ready(function() { $("form :input.required").each(function() { var $required=$("<strong class=high>*</strong>"); $(this).parent().append($required); }); $("form :input").blur(function() { var $parent=$(this).parent(); $parent.find(".formtips").remove(); if($(this).is("#username")){ if(this.value==""||this.value.length<6){ var errorMsg='请输入至少六位的用户名'; $parent.append('<span class="formtips onError">'+errorMsg+'</span>'); }else{ var okMsg='输入正确'; $parent.append('<span class="formtips onSuccess">'+okMsg+'</span>'); return false; } } if($(this).is("#email")){ if(this.value==""||(this.value!==""&&!/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value))){ var errorMsg='请输入正确的E—mail地址'; $parent.append('<span class="formtips onError">'+errorMsg+'</span>'); }else{ var okMsg='输入正确'; $parent.append('<span class="formtips onSuccess">'+okMsg+'</span>'); } } }); $("#send").click(function() { $("form .required:input").trigger('blur'); var numError=$("form .onError").length; if(numError){ return false; } alert("注册成功,密码已发到你的邮箱,请查收。"); }); }); </script> </head> <body> <form method="post" action=""> <p class="int"> <label for="username">用户名:</label> <input type="text" id="username" class="required" /> </p> <p class="int"> <label for="email">邮箱:</label> <input type="text" id="email" class="required" /> </p> <p class="int"> <label for="personinfo">个人资料:</label> <input type="text" id="personinfo" /> </p> <p class="sub"> <input type="submit" value="提交" id="send"/><input type="reset" id="res"/> </p> </form> </body> </html>
Summary
The above is the detailed content of Example code for jQuery form validation. For more information, please follow other related articles on the PHP Chinese website!