This tutorial concerns about creating a login form and validating it using jQuery. The prestored username, password are verified from database whether both exist’s or not in our database, this is done using PHP script .
Here we have created a simple HTML form with two fields- Email and Password. jQuery validation is used for email and password to ensure that they are valid or not .
You can download the zip file or simply follow our code and edit it to use.
-:See Also:-
HTML File: login.html
Given below our complete HTML code , copy to use it.
<!DOCTYPE html>
<html>
<head>
<title>Login Form Using jQuery - Demo Preview</title>
<meta name="robots" content="noindex, nofollow">
<!-- Include CSS File Here -->
<link rel="stylesheet" href="css/style.css"/>
<!-- Include CSS File Here -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="js/login.js"></script>
</head>
<body>
<div class="container">
<div class="main">
<form class="form" method="post" action="#">
<h2>Create Login Form Using jQuery</h2>
<label>Email :</label>
<input type="text" name="demail" id="email">
<label>Password :</label>
<input type="password" name="password" id="password">
<input type="button" name="login" id="login" value="Login">
</form>
</div>
</div>
</body>
</html>
jQuery File: login.js
Given below our complete jQuery code.
$(document).ready(function(){
$("#login").click(function(){
var email = $("#email").val();
var password = $("#password").val();
// Checking for blank fields.
if( email =='' || password ==''){
$('input[type="text"],input[type="password"]').css("border","2px solid red");
$('input[type="text"],input[type="password"]').css("box-shadow","0 0 3px red");
alert("Please fill all fields...!!!!!!");
}else {
$.post("login.php",{ email1: email, password1:password},
function(data) {
if(data=='Invalid Email.......') {
$('input[type="text"]').css({"border":"2px solid red","box-shadow":"0 0 3px red"});
$('input[type="password"]').css({"border":"2px solid #00F5FF","box-shadow":"0 0 5px #00F5FF"});
alert(data);
}else if(data=='Email or Password is wrong...!!!!'){
$('input[type="text"],input[type="password"]').css({"border":"2px solid red","box-shadow":"0 0 3px red"});
alert(data);
} else if(data=='Successfully Logged in...'){
$("form")[0].reset();
$('input[type="text"],input[type="password"]').css({"border":"2px solid #00F5FF","box-shadow":"0 0 5px #00F5FF"});
alert(data);
} else{
alert(data);
}
});
}
});
});
PHP Script: login.php
Below script executes for matching email and password in database.
<?php
$connection = mysql_connect("localhost", "root", ""); // Establishing connection with server..
$db = mysql_select_db("college", $connection); // Selecting Database.
$email=$_POST['email1']; // Fetching Values from URL.
$password= sha1($_POST['password1']); // Password Encryption, If you like you can also leave sha1.
// check if e-mail address syntax is valid or not
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // sanitizing email(Remove unexpected symbol like <,>,?,#,!, etc.)
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "Invalid Email.......";
}else{
// Matching user input email and password with stored email and password in database.
$result = mysql_query("SELECT * FROM registration WHERE email='$email' AND password='$password'");
$data = mysql_num_rows($result);
if($data==1){
echo "Successfully Logged in...";
}else{
echo "Email or Password is wrong...!!!!";
}
}
mysql_close ($connection); // Connection Closed.
?>
CSS File: style.css
Styling HTML Elements.
/* Below line is used for online Google font */
@import url(http://fonts.googleapis.com/css?family=Droid+Serif);
h2{
text-align: center;
font-size: 24px;
}
hr{
margin-bottom: 30px;
}
div.container{
width: 960px;
height: 610px;
margin:50px auto;
font-family: 'Droid Serif', serif;
position:relative;
}
div.main{
width: 320px;
margin-top: 80px;
float:left;
padding: 10px 55px 40px;
background-color: rgba(187, 255, 184, 0.65);
border: 15px solid white;
box-shadow: 0 0 10px;
border-radius: 2px;
font-size: 13px;
}
input[type=text],[type=password] {
width: 97.7%;
height: 34px;
padding-left: 5px;
margin-bottom: 20px;
margin-top: 8px;
box-shadow: 0 0 5px #00F5FF;
border: 2px solid #00F5FF;
color: #4f4f4f;
font-size: 16px;
}
label{
color: #464646;
text-shadow: 0 1px 0 #fff;
font-size: 14px;
font-weight: bold;
}
#login {
width: 100%;
background: linear-gradient(#22abe9 5%, #36caf0 100%);
border: 1px solid #0F799E;
font-size: 20px;
margin-top: 15px;
padding: 8px;
font-weight: bold;
cursor: pointer;
color: white;
text-shadow: 0px 1px 0px #13506D;
}
#login:hover{
background: linear-gradient(#36caf0 5%, #22abe9 100%);
}
Conclusion:
This was all about creating simple login form with validation and database connectivity. Hope you have liked it, keep reading our other blogs for getting attached with more coding tricks.
27 Replies to “Create Login Form Using jQuery”
Hello there! This article couldn’t be written any better!
Going through this article reminds me of my previous roommate!
He constantly kept preaching about this. I will send this article to him.
Pretty sure he’s going to have a great read. Thanks for sharing!
Hello Isotherm,
Thanks for your appreciation !
Keep reading our other blog posts for getting more coding tricks.
Thanks & Regards,
FormGet Team.
Its not my first time to go to see this site, i am browsing this web site dailly and take good facts from here every day.
Thanks for such a wonderful appreciation !
Hi there, just became alert to your blog through Google, and found that it’s really informative.
I’m gonna watch out for brussels. I’ll appreciate if
you continue this in future. A lot of people will be benefited from your writing.
Cheers!
Ya sure you will be getting these informative blogs in future too.
Thanks for reading !
Regards,
FormGet Team.
A motivating discussion is definitely worth comment.
There’s no doubt that that you need to publish more on this issue,
it might not be a taboo subject but generally people do not discuss such issues.
To the next! All the best!!
Yes Vijay , we will definitely be going to publish such blogs in future too.
Keep following us to catch up new posts.
Regards,
FormGet Team.
What’s up, after reading this awesome post i am as well
glad to share my familiarity here with friends.
Glad to have a reader like you !
Your words matters a lot for us.
Thanks & Regards,
FormGet Team.
I am truly happy to read this web site posts which consists
of plenty of useful data, thanks for providing such data.
Hello BBS,
Thank you so much for appreciation !
Follow us in future to seize more knowledge and coding fun.
Regards,
FormGet Team.
Thanks for a great and simple example. Keep up the good work
Hello Mike,
That sounds really great ! Keep reading our other blog posts for getting more coding tricks.
Catch up new posts from here
Regards,
FormGet Team
A further important feature to ensure that is your web designer makes the website navigation as simple as possible for your
site visitors. S, but when it comes to preparing a world-class website, Web Design Miami stands a level ahead.
Before going to discuss about E-commerce web design, everyone should know about e-commerce
website, and its purposes.
Thanks 🙂
Pls, i need a tutorial on web designing , how can you assist me. Thanks God bless you
thanks…………
send more examples like login forms,web pages………. etc…
thank you sooo much
Hi,
Need help.
I have installed wampserver in my laptop and created database “college”. When i am trying to run the above code,,,, I am not able to execute the php script…
When I enter the email and password and click on submit button,, the internet explorer gives an alert box with the whole php script written on it.
Thanks in advance,
Tarun
When every php code is visible on browser, it means that you forget to use localhost in the url. you are trying to access code directly by double clicking.
Run code like this “http://localhost/projectname/filename.php”
Thanks Neeraj, Yes,, this was the reason,, I changed the fill name’s extension from html to php… and then I had to do some modifications in the php code as mysql_connect , mysql_select_db etc functions have been deprecated now..
Anyways,, thanks for your help.
Great assignment…………..
Please provide same thing in java and .net as well……..
it will be more usefull
Very nice, thanks!
In the file “login.php” I have to code my SQL Connection but isn’t this a risk?
All the files will be stored on my webspace, so everyone can read the login.php file or?
I’m new with webprogramming so sorry for the question and thanks for answers.
Hi this isnt safe by any means. The mysql connect is old any isn’t used anymore. Please have a look inte prepared statement. Its The safest way, and sha1 or md5 is horrible for saftey. Please use bcrypt. Or php’s own called simply passform default.
hi, this forms is soo beautiful, but i ave some problem, when i try it, say me invalid email, or password, where i can modify this value?
This isnt in anyway safe by antar means. Javascript semester Nice but The mysql script is really unsafe
hi!,I like your writing very a lot! share we keep
up a correspondence extra about your post on AOL?
I require an expert in this space to unravel my problem.
May be that’s you! Having a look ahead to look you.