Reputation: 29
I would like to validate my input box for a password using javascript/jquery. The passwords needs to be 9 or more characters. I need to use keyup and count user input. If the input is under 9 characters there should be an error message under the input box. if there is more than 9 characters, then the error message should turn into a submit button. I kind of understand to get length, but I dont think I am doing it right. All help is appreciated. Here is some code:
< script >
function checkInput(value) {
document.getElementById('#password').value.length
var minLength = 9;
if (value.length < minLength) {
//not sure
else {
return true;
}
}
<
/script>
<html>
<head>
<title>Email List</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css">
* {
margin: 0;
padding: 0;
font-family: Arial;
box-sizing: border-box
}
body {
text-align: center;
}
header {
padding: 50px;
color: black;
font-size: 20pt
}
section {
width: 700px;
margin: 0 auto;
}
ul {
margin: 0;
padding: 0;
list-style: none;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}
ul li {
cursor: pointer;
position: relative;
padding: 12px 8px 12px 40px;
background: #FF6D1F;
font-size: 18px;
transition: 0.2s;
text-align: left;
user-select: none;
}
ul li:nth-child(odd) {
background: #FA5B0F;
}
ul li:hover {
background: #FF822E;
}
ul li.checked {
background: #FF822E;
color: #fff;
text-decoration: line-through;
}
ul li.checked::before {
content: '';
position: absolute;
border-color: #fff;
border-style: solid;
border-width: 0 2px 2px 0;
top: 10px;
left: 16px;
transform: rotate(45deg);
height: 15px;
width: 7px;
}
.close {
position: absolute;
right: 0;
top: 0;
padding: 12px 16px 12px 16px;
display: none;
background-color: #f44336;
}
.close:hover {
color: white;
}
form {
padding: 0;
margin-top: 20px;
width: 100%;
overflow: hidden;
}
input {
height: 56px;
line-height: 56px;
margin-right: 0;
display: inline-block;
float: left;
width: 90%;
font-size: 18px;
padding: 12px 8px 12px 40px;
outline: 0;
}
.btn {
display: inline-block;
background-color: green;
width: 10%;
padding: 9.5px;
font-size: 32px;
cursor: pointer;
background-color: #FA5B0F;
}
.btn:hover {
color: white;
background-color: #ff931e;
}
#lblError {
background-color: #000;
color: white;
height: 56px;
line-height: 56px;
display: none;
}
</style>
</head>
<body>
<header>
<h1>Create A New Password</h1>
</header>
<section>
<form>
<input type="password" id="password" placeholder="Enter a password" autocomplete="off">
</form>
<div id="lblError">Please enter a password longer than 9 characters!</div>
</section>
</html>
Feel free to ask for any clarification. Thank you!
Upvotes: 0
Views: 2306
Reputation: 311
You can also try using pure javascript to accomplish this.
var minLength = 9;
var output = document.getElementById("output");
function checkInput(){
var password = document.getElementById("password").value;
if (password.length < minLength) {
output.innerHTML = "Not yet.....";
return false;
}else {
output.innerHTML = "<button>submit</button>";
}
}
<form>
<input type="password" id="password" placeholder="Enter a password" autocomplete="off" onkeyup="checkInput()">
<p id="output"></p>
</form>
Upvotes: 1
Reputation: 177
Use .on("keypress",function(){})
as written to the code below:
$("#password").on("keyup",function(){
if($(this).val().length < 9 && $(this).val().length != 0 ){
$("#lblError").prop("style","display:block");
$("#Submit").prop("style","display:none");
}
else if($(this).val().length == 0){
$("#Submit").prop("style","display:none");
$("#lblError").prop("style","display:none");
}
else{
$("#Submit").prop("style","display:block");
$("#lblError").prop("style","display:none");
}
})
<html>
<head>
<title>Email List</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css">
* {
margin: 0;
padding: 0;
font-family: Arial;
box-sizing: border-box
}
body {
text-align: center;
}
header {
padding: 50px;
color: black;
font-size: 20pt
}
section {
width: 700px;
margin: 0 auto;
}
ul {
margin: 0;
padding: 0;
list-style: none;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}
ul li {
cursor: pointer;
position: relative;
padding: 12px 8px 12px 40px;
background: #FF6D1F;
font-size: 18px;
transition: 0.2s;
text-align: left;
user-select: none;
}
ul li:nth-child(odd) {
background: #FA5B0F;
}
ul li:hover {
background: #FF822E;
}
ul li.checked {
background: #FF822E;
color: #fff;
text-decoration: line-through;
}
ul li.checked::before {
content: '';
position: absolute;
border-color: #fff;
border-style: solid;
border-width: 0 2px 2px 0;
top: 10px;
left: 16px;
transform: rotate(45deg);
height: 15px;
width: 7px;
}
.close {
position: absolute;
right: 0;
top: 0;
padding: 12px 16px 12px 16px;
display: none;
background-color: #f44336;
}
.close:hover {
color: white;
}
form {
padding: 0;
margin-top: 20px;
width: 100%;
overflow: hidden;
}
input {
height: 56px;
line-height: 56px;
margin-right: 0;
display: inline-block;
float: left;
width: 90%;
font-size: 18px;
padding: 12px 8px 12px 40px;
outline: 0;
}
.btn {
display: inline-block;
background-color: green;
width: 10%;
padding: 9.5px;
font-size: 32px;
cursor: pointer;
background-color: #FA5B0F;
}
.btn:hover {
color: white;
background-color: #ff931e;
}
#lblError {
background-color: #000;
color: white;
height: 56px;
line-height: 56px;
display: none;
}
#Submit {
color: white;
height: 56px;
line-height: 56px;
display: none;
}
</style>
</head>
<body>
<header>
<h1>Create A New Password</h1>
</header>
<section>
<form>
<input type="password" id="password" placeholder="Enter a password" autocomplete="off">
</form>
<div id="lblError">Please enter a password longer than 9 characters!</div>
<div id="Submit"><input type="submit"></div>
</section>
</html>
Feel free to ask for any clarification.
Upvotes: 1