Thursday, May 26, 2011

Form Validation using jQuery

&lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt
<html&gt
<head&gt
<title>Simple Form Validation&lt/title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script&gt
&ltscript type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"&gt&lt/script&gt

<script type="text/javascript"&gt
$(document).ready(function() {
$("#form1").validate({
rules: {
name: "required",// simple rule, converted to {required:true}
email: {// compound rule
required: true,
email: true
},
url: {
url: true
},
comment: {
required: true
}
},
messages: {
comment: "Please enter a comment."
}
});
});
&lt/script&gt

<style type="text/css">
* { font-family: Verdana; font-size: 11px; line-height: 14px; }
.submit { margin-left: 125px; margin-top: 10px;}
.label { display: block; float: left; width: 120px; text-align: right; margin-right: 5px; }
.form-row { padding: 5px 0; clear: both; width: 700px; }
label.error { width: 250px; display: block; float: left; color: red; padding-left: 10px; }
input[type=text], textarea { width: 250px; float: left; }
textarea { height: 50px; }
</style&gt
</head&gt

<body>
<form id="form1" method="post" action="">
<div class="form-row"><span class="label">Name *&lt/span><input type="text" name="name" /&gt&lt/div>
<div class="form-row"&gt&ltspan class="label">E-Mail *&lt/span&gt&ltinput type="text" name="email" /></div>
<div class="form-row"&gt
<div class="form-row"&gt&ltinput class="submit" type="submit" value="Submit">
</form>
</body>
</html>

No comments:

Post a Comment