Skip to main content

jQuery form Validation

jQuery form Validation


What is jQuery :
1-jQuery is a fast and concise JavaScript Library created by John Resig in 2006 with a nice motto -
2-Write less, do more.
3-jQuery simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.
Following feature of jQuery :
1-DOM manipulation − The jQuery made it easy to select DOM elements, traverse them and modifying their content by using cross-browser open source selector engine called Sizzle.
2-Event Handling : The jQuery offers an elegant way to capture a wide variety of events,such as click,over event,
3-Ajax Support :
jQuery support ajax technology,
4-Lightweight :he jQuery is very lightweight library - about 19KB in size ( Minified and gzipped ).
5-Animations − The jQuery comes with plenty of built-in animation effects.
6-Cross Browser Support − The jQuery has cross-browser support, and works well in IE and all.


Demo link :

http://freeteachnology.hol.es/jquery_validation/


Code :




Following is best example of form validation using jquery library, using jquery validation used following file :
1-jquery.validate.js,
2-jquery-3.1.1.js,


Now,simply feature file sturcture are dived into following ways :
create file structure :
1-home.php,
2-error.css,
3-js_signup.js,
4-action.php,
5-jquery.validate.js,


1-home.php
it file include cdn link of bootstrp css,js and validation,jquery-3.1.1,
Code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Jquery validation</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<link href="error.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="jquery.validate.js"></script>
<script src="js_signup.js"></script>
</head>


in body section all html filed are there.
Home.php file include follwoing code :


Code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Jquery validation</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<link href="error.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="jquery.validate.js"></script>
<script src="js_signup.js"></script>
</head>
<body>
<div class="container">
<div class="alert alert-info">
<strong>Hi..</strong> jQuery Form Validation.
</div>
</div>
<div class="container text-center card">
<div class="col-md-6">
<form action="action.php" method="post" id="signupFrom" name="signupFrom" accept-charset="utf-8">
<label for="name">First Name</label>
<input type="text" name="fname" id="fname" class="form-control" placeholder="Enter First Name..." />
<div class="clearfix"></div>
<label for="name">Last Name</label>
<input type="text" name="lname" id="lname" class="form-control" placeholder="Enter Last Name..." />
<div class="clearfix"></div>
<label for="contact">Mobile No</label>
<input type="text" name="mobile" id="mobile" class="form-control" placeholder="Enter your mobile no..." />
<div class="clearfix"></div>
<label for="contact">DOB</label>
<input type="text" name="dob" id="dob" class="form-control" placeholder="Enter dob no..." />
<div class="clearfix"></div>
<label for="contact">Pin code</label>
<input type="text" name="pincode" id="pincode" class="form-control" placeholder="Pin Code.." />
<div class="clearfix"></div>
<label for="msg">Address</label>
<textarea name="address" id="address" cols="40" rows="10" class="form-control" rows="3" placeholder="Enter your Address..."></textarea>
<div class="clearfix"></div>
<label for="msg">Sex</label>
<select class="form-control" name="sex" id="sex">
<option value="">--select--</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<div class="clearfix"></div></br>
<label for="msg">Nationality</label>
<div class="clearfix"></div>
<input type="radio" name="nationality" />Indian
<input type="radio" name="nationality" />Other
<div class="clearfix"></div>
<div for="nationality" generated="true" style="display:none" class="error"></div>
<label for="name">Email</label>
<input type="text" name="email" id="email" class="form-control" placeholder="Enter Email..." />
<div class="clearfix"></div>
<label for="name">Username</label>
<input type="text" name="username" id="username" class="form-control" placeholder="Enter Username..." />
<div class="clearfix"></div>
<label for="name">Password</label>
<input type="password" name="password" id="password" class="form-control" placeholder="Password" />
<div class="clearfix"></div>
<label for="name">Confirm Password</label>
<input type="password" name="cpassword" id="cpassword" class="form-control" placeholder="Confirm Password" />
<div class="clearfix"></div>
<label for="name">Profile Pic</label>
<input type="file" name="profile" id="profile" class="form-control" />
<div class="clearfix"></div>
<input type="checkbox" id="warning" class="badgebox"> i agree term and condition
<div class="clearfix"></div></br>
<button type="submit" name="submit" id="submit" class="btn btn-primary">Singup</button>
</form>
</div>
</div>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
<br>
<!--Start:: if success ajax call then show message -->
<div id="demo"></div>
<!--End:: if success ajax call then show message -->
</body>
</html>


2-action.php
action.php file is validation is true then call action.php and print all submit data.



Code:

    
<?php
/*
*Post value::-
*/
echo '<pre>';
print_r($_POST);
print_r($_FILES);
die;


?>

3-error.css :
its include css tags thoese tags are used for error message.
4-js_signup.js:
using js validation library define validation rules of html field. Validation rules set validation message in following ways :
Code :
    
jQuery(document).ready(function () {
$("#signupFrom").validate({
rules: {
fname: {
required: true,
},
lname: {
required: true,
},
mobile: {
required: true,
number:true,
maxlength:10,
minlength:10
},
dob:{
required: true,
},
pincode: {
required: true,
number:true,
maxlength:6,
minlength:6
},
address: {
required: true,
},
sex:{
required: true,
},
nationality: {
required: true,
},
email:{
required: true,
email:true
},
username:{
required: true,
},
password:{
required: true,
maxlength:20,
minlength:10
},
cpassword:{
required: true,
maxlength:20,
minlength:10,
equalTo:"#password"
},
profile: {
required: true,
accept: "jpg|jpeg|png",
}
},
messages: {
fname: {
required: "* Please enter first name.",
},
lname: {
required: "* Please enter last name.",
},
mobile: {
required: "* Please enter mobile no.",
number: "* Please enter only number.",
minlength: "* Mobile no should be minimum 10 digit.",
maxlength: "* Mobile no should be maximum 10 digit."
},
dob: {
required: "* Please enter dob."
},
pincode: {
required: "* Please enter Pincode.",
number: "* Please enter only number.",
minlength: "* Pincode should be minimum 6 digit.",
maxlength: "* Pincode should be maximum 6 digit."
},
address: {
required: "* Please enter address."
},
sex:{
required: "* Please enter address."
},
nationality:{
required: "* Please select nationality."
},
email:{
required: "* Please enter email.",
email:"* Enter email id is correct."
},
username:{
required: "* Please enter username."
},
password:{
required: "* Please enter password.",
minlength: "* Password should be minimum 10 digit.",
maxlength: "* Password should be maximum 20 digit."
},
cpassword:{
required: "* Please enter password.",
minlength: "* Password should be minimum 10 digit.",
maxlength: "* Password should be maximum 20 digit.",
equalTo:"* Enter password and confirm password are not match."
},
profile: {
required: "* Please upload profile image.",
accept: "* Please select jpg, jpeg, png file extension."
}
},
});
});

Above code if form validate can set then define form name and thoese form name set in validation :
Code:
$("#signupFrom").validate({ });

Comments

Popular posts from this blog

using PDO database connection add,update,delete,edit operation

PDO advantage : 1-Object Oriented 2-Bind parameters in statements (security) 3-Allows for prepared statements and rollback functionality (consistency) 4-Throws catcheable exceptions for better error handling (quality) 5-Exception mode; no need to check error state after each API call. It's best to tell PDO how you'd like the data to be fetched. You have the following options: 1-PDO::FETCH_ASSOC: returns an array indexed by column name. 2-PDO::FETCH_BOTH: (default):returns an array indexed by both column name and number. 3-PDO::FETCH_BOUND:Assigns the values of your columns to the variables set with the ->bindColumn() method. 4-PDO::FETCH_CLASS: Assigns the values of your columns to properties of the named class. It will create the properties if matching properties do not exist. 5-PDO::FETCH_INTO:Updates an existing instance of the named class. 6-PDO::FETCH_LAZY: Combines. 7-PDO::FETCH_BOTH/PDO:FETCH_OBJ, creating the object variable names as t...

Profile Share Fixing the Thumbnail Image, Title and Description for Shared Links

Profile Share Fixing the Thumbnail Image, Title and Description for Shared Links user want to share any information then use following code  and read step by step Profile Share Fixing the Thumbnail Image, Title and Description for Shared Links if you want share profile on following social link : 1-Facebook 2-twitter.com 3-LinkedIn 4-google +, Code link : https://drive.google.com/open?id=1IzTZZh_0euDqFSHL_vPRiQePlNTw3h-q Demo link : http://freeteachnology.hol.es/socialshare/ To modify a page's thumbnail image, description, and additional metadata for these services, you can provide meta tags in the HTML code of the page.Implementing Open Graph Meta Tags You can implement meta tags in a number of ways. In content management systems might  be allow you to modify a page's meta tags , then use following code in meta section of your project code, <link href="bootstrap.min.css" rel="stylesheet"> <link href="bootstrap-tour.m...

GUID for globally unique identifier

How to create GUID in php 1-guid stands for globally unique identifier generally used to create random unique strings in php, create access token in php 2-Mostly use of GUID for generating access token, generate unique id, generating unique string in php. Using this article how to create guide in php you can create a random string for any use to keep unique 3-GUID consists of alphanumeric characters only and is grouped in five groups separated by hyphens as seen in this example: 3F2504E0-4F89-11D3-9A0C-0305E82C3301 Eg:- <?php /** * Generate Globally Unique Identifier (GUID) * E.g. 2EF40F5A-ADE8-5AE3-2491-85CA5CBD6EA7 * * @param boolean $include_braces Set to true if the final guid needs * to be wrapped in curly braces * @return string */ function generateGuid($include_braces = false) { if (function_exists('com_create_guid')) { if ($include_braces === true) { return com_create_guid(); } else { return substr(com_cr...