jQuery provides a validation plugin or library to validate your form, all you have to do is just import it in your script and use it in a simple way.

But, here we will show you a simple programming approach to validate your form using jQuery without importing  jQuery validation library or plugin.

Below example contains an HTML form with some fields, we used following Regular Expressions (RegEx) in our jQuery codes to set validation rules :

RegEx for Name field:


RegEx for Email field:


RegEx for Address field:


RegEx for Postal Zip field:


About Regular Expression

It pre-defines characters in the particular form field. So, when user entered data into the fields, regular expression matches the data.

The field will accept that character which the regular expression allow it to do.

If the character does not match, then alert will be display that notifies about wrong input to user. You can check out below, what these characters means:

. : Matches any single charcter except a new line

+ : Matches the preceding character or repeated character.

$ : Matches character at the end of the line.

.: Matches only period.

^ : Matches the beginning of a line or string.

: Escapes a special character.

- : Range indicator. [a-z, A-Z]

-: Escapes a special character.(e.g. escaping - by -)

[0-9] : It matches digital number from 0-9.

[a-z] : It matches characters from lowercase ‘a’ to lowercase ‘z’.

[A-Z] : It matches characters from uppercase ‘A’ to lowercase ‘Z’.

w: Matches a word character and underscore. (a-z, A-Z, 0-9, _).

W: Matches a non word character (%, #, @, !).

{M, N} : Donates minimum M and maximum N value.

In our example, jQuery code get executes for each form field, which matches the user input value with above defined RegEx. If, it got perfect match then function returns true otherwise, false

form validation using jQuery

To learn more, we have our complete HTML,  jQuery and Regular Expression references given below.

HTML file: formvalid.html

<!DOCTYPE html>
<title>Form Validation jQuery:Demo Preview</title>
<meta content="noindex, nofollow" name="robots">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="formvalid.css" rel="stylesheet">
<script src="formvalid.js"></script>
<div class="main">
<h1>Form Validation Using jQuery</h1>
<div id="form">
<p id="head"></p> <!-- This Segment Displays The Validation Rule -->
<h2>Java script form validation</h2>
<form action="" method="post"> <!-- Form Starts From Here -->
<label>Full Name:</label>
<input id='firstname' type='text'>
<p id="p1"></p> <!--This Segment Displays The Validation Rule For Name-->
<label>Username(6-8 characters):</label>
<input id='username' type='text'>
<p id="p2"></p> <!-- This Segment Displays The Validation Rule For User Name -->
<input id='email' type='text'>
<p id="p3"></p> <!-- This Segment Displays The Validation Rule For Email -->
<select id='state'>
<option>Please Choose</option>
<p id="p4"></p> <!-- This Segment Displays The Validation Rule For Selection -->
<input id='addr' type='text'>
<p id="p5"></p> <!-- This Segment Displays The Validation Rule For Address -->
<label>Zip Code:</label>
<input id='zip' type='text'>
<p id="p6"></p> <!-- This Segment Displays The Validation Rule For Zip -->
<button id="submit">Check Form</button>

jQuery File: formvalid.js

$(document).ready(function() {
$('#submit').click(function(e) {
// Initializing Variables With Form Element Values
var firstname = $('#firstname').val();
var addr = $('#addr').val();
var zip = $('#zip').val();
var state = $('#state').val();
var username = $('#username').val();
var email = $('#email').val();
// Initializing Variables With Regular Expressions
var name_regex = /^[a-zA-Z]+$/;
var email_regex = /^[w-.+]+@[a-zA-Z0-9.-]+.[a-zA-z0-9]{2,4}$/;
var add_regex = /^[0-9a-zA-Z]+$/;
var zip_regex = /^[0-9]+$/;
// To Check Empty Form Fields.
if (firstname.length == 0) {
$('#head').text("* All fields are mandatory *"); // This Segment Displays The Validation Rule For All Fields
return false;
// Validating Name Field.
else if (!firstname.match(name_regex) || firstname.length == 0) {
$('#p1').text("* For your name please use alphabets only *"); // This Segment Displays The Validation Rule For Name
return false;
// Validating Username Field.
else if (!(username.length >= 6 && username.length <= 8) || username.length == 0) {
$('#p2').text("* Please enter between 6 and 8 characters *"); // This Segment Displays The Validation Rule For Username
return false;
// Validating Email Field.
else if (!email.match(email_regex) || email.length == 0) {
$('#p3').text("* Please enter a valid email address *"); // This Segment Displays The Validation Rule For Email
return false;
// Validating Select Field.
else if (state == "Please Choose") {
$('#p4').text("* Please Choose any one option"); // This Segment Displays The Validation Rule For Selection
return false;
// Validating Address Field.
else if (!addr.match(add_regex) || addr.length == 0) {
$('#p5').text("* For Address please use numbers and letters *"); // This Segment Displays The Validation Rule For Address
return false;
// Validating Zip Field.
else if (!zip.match(zip_regex) || zip.length == 0) {
$('#p6').text("* Please enter a valid zip code *"); // This Segment Displays The Validation Rule For Zip
return false;
} else {
alert("Form Submitted Successfuly!");
return true;

CSS File: formvalid.css

@import "http://fonts.googleapis.com/css?family=Droid+Sans";
@import "http://fonts.googleapis.com/css?family=Roboto+Slab";
body {
font-family:'Droid Sans',sans-serif;
.main {
margin:10px auto
div#form {
padding:20px 0 20px 20px;
border:1px dashed #000;
h2 {
font-family:'Roboto Slab',serif
p {
h1 {
input {
border:1px solid #e3e3e3
#firstname {
#username {
#email {
#state {
#addr {
#zip {
#submit {
border:1px solid #000

Pabbly Form Builder


Thus, using above mentioned regular expressions and jQuery codes, you can define your own validation rules for your forms. Keep reading our other blogs.

You may also like –