<head> <meta charset="utf-8"> <title>Validate form trong Jquery</title> <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script> <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js" type="text/javascript"></script> <style> label.error{ color: red; } </style> </head>
Trong thẻ body khai báo form như sau :
<div id="main"> <form method="get" id="demoForm" action=""> <p> <label>Username</label> <input name="user" type="text" /> </p> <p> <label>Password</label> <input id="password" type="password" name="password" /> </p> <p> <label>Re-Password</label> <input id="re-password" type="password" name="re-password" /> </p> <p> <input type="submit" value="Register" id="register"/> </p> </form> </div>
Dưới đây là các rules có sẵn của Jquery validate
Tiến hành code validate cho form
Demo: