Twitter 表单验证
XML/HTML Code
- <form class="form-horizontal" role="form" id="register">
- <div class="form-group">
- <label for="firstname" class="col-sm-4 control-label">Firstname :</label>
- <div class="col-sm-6">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span>
- </span>
- <input type="text" class="form-control" name="firstname" placeholder="Firstname">
- </div>
- <label class="error" for="firstname"></label>
- </div>
- </div>
- <div class="form-group">
- <label for="lastname" class="col-sm-4 control-label">Lastname :</label>
- <div class="col-sm-6">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span>
- </span>
- <input type="text" class="form-control" name="lastname" placeholder="Lastname">
- </div>
- <label class="error" for="lastname"></label>
- </div>
- </div>
- <div class="form-group">
- <label for="username" class="col-sm-4 control-label">Username :</label>
- <div class="col-sm-6">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span>
- </span>
- <input type="text" class="form-control" name="username" placeholder="Username">
- </div>
- <label class="error" for="username"></label>
- </div>
- </div>
- <div class="form-group">
- <label for="Password1" class="col-sm-4 control-label">Password :</label>
- <div class="col-sm-6">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span>
- </span>
- <input type="password" class="form-control" id="password1" name="password1" placeholder="Password">
- </div>
- <label class="error" for="password1"></label>
- </div>
- </div>
- <div class="form-group">
- <label for="password2" class="col-sm-4 control-label">Confirm Password :</label>
- <div class="col-sm-6">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span>
- </span>
- <input type="password" class="form-control" name="password2" placeholder="Confirm Password">
- </div>
- <label class="error" for="password2"></label>
- </div>
- </div>
- <div class="form-group">
- <label for="inputEmail" class="col-sm-4 control-label">Email :</label>
- <div class="col-sm-6">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
- </span>
- <input type="email" class="form-control" name="inputEmail" placeholder="Email">
- </div>
- <label class="error" for="inputEmail"></label>
- </div>
- </div>
- <div class="form-group">
- <label for="phone" class="col-sm-4 control-label">Phone :</label>
- <div class="col-sm-6">
- <div class="input-group">
- <span class="input-group-addon"><span class="glyphicon glyphicon-phone-alt"></span>
- </span>
- <input type="tel" class="form-control" name="phone" placeholder="Valid contact number">
- </div>
- <label class="error" for="phone"></label>
- </div>
- </div>
- <div class="form-group">
- <div class="col-sm-offset-4 col-sm-6">
- <div class="checkbox">
- <label>
- <input type="checkbox" name="agree">Please accept our policy
- </label>
- </div>
- <label class="error" for="agree"></label>
- </div>
- </div>
- <div class="form-group">
- <div class="col-sm-offset-4 col-sm-6">
- <button type="button" class="btn btn-default signup">Signup</button>
- <input type="hidden" name="register" />
- <label class="result"></label>
- </div>
- </div>
- </form>
JavaScript Code
- <script>
- $().ready(function () {
- $('form#register').validate({ // initialize the plugin
- success: function(label) {
- label.text("ok!").addClass("success"); //if valid add class
- },
- validClass: "success",
- rules: {
- firstname: "required",
- lastname: "required",
- username: {
- required: true,
- minlength: 2
- },
- password1: {
- required: true,
- minlength: 5
- },
- password2: {
- required: true,
- minlength: 5,
- equalTo: "#password1"
- },
- inputEmail: {
- required: true,
- email: true
- },
- agree: "required",
- phone: {
- required: true,
- digits: true,
- minlength: 10,
- maxlength: 10
- }
- },
- messages: {
- firstname: "Enter your firstname",
- lastname: "Enter your lastname",
- username: {
- required: "Please Enter username",
- minlength: "Enter at least 2 characters"
- },
- password1: {
- required: "Please confirm your password",
- minlength: "Your password must be at least 5 characters long"
- },
- password2: {
- 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",
- phone: {
- required: "Please enter phone number",
- digits: "Please enter only digits",
- minlength: "Please specify a valid phone number",
- maxlength: "Please specify a valid phone number"
- }
- }
- });
- $("button.signup").click(function () {
- var register = $('#register').valid(); // Checking valid form
- if (register) {
- $.ajax({
- type: "POST",
- url: "process.php",
- data: $('form#register').serialize(), //serialize form
- success: function (msg) {
- $("label.result").html(msg); // Get status from process.php
- }
- });
- }
- });
- });
- </script>
原文地址:http://www.freejs.net/article_biaodan_405.html