In: Computer Science
Here, I detail the validation rules that you should implement and the error messages that must be shown for invalid inputs: For login page:
username input is required. If it is empty, show error message of “Please enter your username.”
username input should be at least 5 character. If it is less that 5 character, show error message that “Please enter at least 5 characters.”
password input is required. If it is empty, show error message of “Please enter your password.”
Use a jQuery library for form validation to do so.
The HTML code I have is:
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<meta charset="utf-8">
<script src="loginform.js"></script>
</head>
<style>
.header {
text-align: center;
font-size: 36px;
background-color: #b1d1d1;
}
input[type=text], select {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type="password"],select {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button[type=login] {
width: 45%;
background-color: #039be5;
color: white;
padding: 12px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
text-align: center;
}
input[type=login]:hover {
opacity: 0.5;
}
div {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
</style>
<body>
<div class="header">
Login Form
</div>
<div class= "FormInfo">
<form action="/action_page.php">
<strong><br>
<label for="username">Username</label><br>
<input type="text" id="username" name="username"
placeholder="Enter your Username"><br>
<label
for="password">Password</label><br>
<input type="password" id="password" name="password"
placeholder="Enter your Password" width: 100%>
<br>
<button type="login" onclick="check(this.form)" value="login">Login</button>
<label><input id="rememberme" name="rememberme"
value="remember" type="checkbox" input checked/>
Remember me</label>
</form>
</div>
</body>
</html>
logiinform.js
function check(form)
{
var uid = form.username;
var passid = form.password;
if(userid_validation(uid,5))
{
if(passid_validation(passid))
{
return
true;
}
}
return false;
}
function userid_validation(uid,mx)
{
var uid_len = uid.value.length;
if (uid_len == 0 )
{
alert("Please enter your username");
uid.focus();
return false;
}
if ( uid_len < mx)
{
alert("Please enter at least 5 characters.");
uid.focus();
return false;
}
return true;
}
function passid_validation(passid)
{
var uid_len = passid.value.length;
if (uid_len == 0 )
{
alert("Please enter your
password.");
passid.focus();
return false;
}
return true;
}
___________________________________________________________________________________________
login.html
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<meta charset="utf-8">
<script src="loginform.js"></script>
</head>
<style>
.header {
text-align: center;
font-size: 36px;
background-color: #b1d1d1;
}
input[type=text], select {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type="password"],select {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button[type=login] {
width: 45%;
background-color: #039be5;
color: white;
padding: 12px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
text-align: center;
}
input[type=login]:hover {
opacity: 0.5;
}
div {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
</style>
<body>
<div class="header">
Login Form
</div>
<div class= "FormInfo">
<form action="/action_page.php">
<strong><br>
<label for="username">Username</label><br>
<input type="text" id="username" name="username"
placeholder="Enter your Username"><br>
<label
for="password">Password</label><br>
<input type="password" id="password" name="password"
placeholder="Enter your Password" width: 100%>
<br>
<button type="login" onclick="return check(this.form)" value="login">Login</button>
<label><input id="rememberme" name="rememberme"
value="remember" type="checkbox" input checked/>
Remember me</label>
</form>
</div>
</body>
</html>
Note: need to change onclick="check(this.form)" to onclick="return check(this.form)"
____________________________________________OUTPUT________________________________________