jQuery Form Validation

Validate your form using jquery validation.

below is an example:

=====================================================================================

<html>
<head>

<link href=”//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css” rel=”stylesheet”>
<script src=”//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js”></script>

<!– Load jQuery and the validate plugin –>
<script src=”//code.jquery.com/jquery-1.9.1.js”></script>
<script src=”//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js”></script>
<style type=”text/css”>
.label {
width:100px;
text-align:right;
float:left;
padding-right:10px;
font-weight:bold;
}
#register-form label.error {
color:#FB3A3A;
font-weight:bold;
}
h1 {
font-family: Helvetica;
font-weight: 100;
color:#333;
padding-bottom:20px;
}
</style>
<!– jQuery Form Validation code –>
<script>

// When the browser is ready…
jQuery(function() {

// Setup form validation on the #register-form element
jQuery(“#register-form”).validate({

// Specify the validation rules
rules: {
firstname: “required”,
lastname: “required”,
email: {
required: true,
email: true
},
password: {
required: true,
minlength: 5
},
agree: “required”
},

// Specify the validation error messages
messages: {
firstname: “Please enter your first name”,
lastname: “Please enter your last name”,
password: {
required: “Please provide a password”,
minlength: “Your password must be at least 5 characters long”
},
email: “Please enter a valid email address”,
agree: “Please accept our policy”
},

submitHandler: function(form) {
form.submit();
}
});

});

</script>
</head>
<body>
<h1>Register here</h1>

<!– The form that will be parsed by jQuery before submit –>
<form action=”” method=”post” id=”register-form” novalidate=”novalidate”>

<div class=”label”>First Name</div><input type=”text” id=”firstname” name=”firstname” /><br />
<div class=”label”>Last Name</div><input type=”text” id=”lastname” name=”lastname” /><br />
<div class=”label”>Email</div><input type=”text” id=”email” name=”email” /><br />
<div class=”label”>Password</div><input type=”password” id=”password” name=”password” /><br />
<div style=”margin-left:140px;”><input type=”submit” name=”submit” value=”Submit” /></div>

</form>

</body>
</html>

============================================================================================

No Comments

Post a Comment