Basic JQuery Form Validation Example (2mins) - SitePoint
Basic JQuery Form Validation Example (2mins) - SitePoint
com/)
This popular article was updated on 18th August, 2016 to correct some minor errors and re ect the current version of the
library. Comments pertaining to the old article were deleted.
This tutorial shows you how to set up a basic form validation with jQuery, demonstrated by a registration form.
We’re going to use the jQuery Validation Plugin (https://jqueryvalidation.org/) to validate our form. The basic principle of this
plugin is to specify validation rules and error messages for HTML elements in JavaScript.
EDIT ON
HTML SCSS JS Result
Registration
First Name
John
Last Name
Doe
Email
john@doe.com
Password
●●●●●
Register
This example is part of the article Basic jQuery Form Validation Example on
SitePoint by Julian Motz.
Create a new HTML le named index.html and include jQuery before the closing </body> tag:
( )
(https://www.sitepoint.com/)
<!-- Change the "src" attribute according to your installation path -->
<script src="vendor/jquery/dist/jquery.min.js"></script>
<!-- Change the "src" attribute according to your installation path -->
<script src="vendor/jquery-validation/dist/jquery.validate.min.js"></script>
1 First name
2 Last name
3 Email
4 Password
<div class="container">
<h2>Registration</h2>
<form action="" name="registration">
<label for="email">Email</label>
<input type="email" name="email" id="email" placeholder="john@doe.com"/>
<label for="password">Password</label>
<input type="password" name="password" id="password" placeholder="●●●●●"/>
<button type="submit">Register</button>
</form>
</div>
( )
(https://www.sitepoint.com/)
When integrating this into a real application, don’t forget to ll in the action attribute, to make sure the form is submitted to
the correct destination.
( )
(https://www.sitepoint.com/)
@import url("https://fonts.googleapis.com/css?family=Open+Sans");
/* Styles */
* {
OFFER ENDS IN 09 HRS 20 MINS 22 SECS
margin: 0;
Start Learning For $9 (/Premium/L/Join?Ref_source=Sitepoint&Ref_medium=Noticebar&Ref_campaign=Basic-Jquery-Form-Validation-
padding: 0;
Tutorial&Ref_content=Javascript)
}
body {
font-family: "Open Sans";
font-size: 14px;
}
.container {
width: 500px;
margin: 25px auto;
}
form {
padding: 20px;
background: #2c3e50;
color: #fff;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
}
form label,
form input,
form button {
border: 0;
margin-bottom: 3px;
display: block;
width: 100%;
}
form input {
height: 25px;
line-height: 25px;
background: #fff;
color: #000;
padding: 0 6px;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
form button {
height: 30px;
line-height: 30px;
background: #e67e22;
color: #fff;
margin-top: 10px;
cursor: pointer;
}
form .error {
color: #ff0000;
}
If you’re using Compass (http://compass-style.org/) (SCSS) you can alternatively use the following to style the form:
( )
(https://www.sitepoint.com/)
@import "compass";
@import url("https://fonts.googleapis.com/css?family=Open+Sans");
/* Color settings */
OFFER ENDS IN 09 HRS 20 MINS 22 SECS
$formBg: #2c3e50;
Start Learning For $9 (/Premium/L/Join?Ref_source=Sitepoint&Ref_medium=Noticebar&Ref_campaign=Basic-Jquery-Form-Validation-
$formColor: #fff;
Tutorial&Ref_content=Javascript)
$inputBg: #fff;
$inputColor: #000;
$buttonBg: #e67e22;
$buttonColor: #fff;
$errorMsgColor: #ff0000;
/* Styles */
* {
margin: 0;
padding: 0;
}
body {
font-family: "Open Sans";
font-size: 14px;
}
.container {
width: 500px;
margin: 25px auto;
}
form {
padding: 20px;
background: $formBg;
color: $formColor;
@include border-radius(4px);
label,
input,
button {
border: 0;
margin-bottom: 3px;
display: block;
width: 100%;
}
input {
height: 25px;
line-height: 25px;
background: $inputBg;
color: $inputColor;
padding: 0 6px;
@include box-sizing(border-box);
}
button {
height: 30px;
line-height: 30px;
background: $buttonBg;
color: $buttonColor;
margin-top: 10px;
cursor: pointer;
}
.error {
color: $errorMsgColor;
}
}
( )
(https://www.sitepoint.com/)
Note the styles for .error, which will be used for validation error messages.
<script src="js/form-validation.js"></script>
Conclusion
That’s it, you’re done! Now you have an idea how to set up form validation with jQuery. Please keep in mind that this doesn’t
replace server-side validation. It’s still possible for a malicious user to manipulate or bypass the validation rules (e.g. using the
browser’s developer tools).
( )
(https://www.sitepoint.com/)
Related Articles
jQuery Ajax Validation Use the Remote Rule (https://www.sitepoint.com/jquery-ajax-validation-remote-rule/)
10 jQuery Form Validation Plugins OFFER ENDS IN 09 HRS 20 MINS 22 SECS
(https://www.sitepoint.com/10-jquery-form-validation-plugins/)
Julian is a passionate software developer currently focusing on frontend technologies and loves open source.
(http
Sam Deering (https://www.sitepoint.com/author/sdeering/) (https://twitter.com/samdeering)
(https://plus.google.com/104487173540464647216/) (https://github.com/sdeering)
Sam Deering is a Front-end Web Developer who specialises in JavaScript & jQuery. Sam is driven and passionate about sharing his knowledge to educate
others.
Versioning (/versioning/) Press Room (/press/) FAQ (https://sitepoint.zendesk.com/hc/en-us) Privacy Policy (/legals/#privacy)
References (/html-css/css/)
Connect