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.
Answer from Jan Lois on Stack OverflowUse .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.
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>
onKeyValidateis an okay name, but a better name could bevalidateKeypress.It seems very silly to store a RegExp as a string, and then construct it every time. Why not just declare
var alpha = /[ A-Za-z]/?keyCharsappears to check against\x00, the null character, and\x08, the backspace character. Neither of these can ever be passed toonKeypress, so you can just take it out.The standard way to get the character code is
event.which || event.keyCode.eventis a global; I don't think you need to pass it in.
Here's a proposed rewrite:
var alpha = /[ A-Za-z]/;
var numeric = /[0-9]/;
var alphanumeric = /[ A-Za-z0-9]/;
function validateKeypress(validChars) {
var keyChar = String.fromCharCode(event.which || event.keyCode);
return validChars.test(keyChar) ? keyChar : false;
}
The HTML will have to change to onkeypress="validateKeypress(alpha);".
The thing that I was able to pick out, and it's more of a nitpick type of things is that you should turn your last if statement around
if (!validChars.test(keychar) && !keyChars.test(keychar)) {
return false
} else{
return keychar;
}
should look like this
if (validChars.test(keychar) && keyChars.test(keychar)) {
return keychar;
} else {
return false;
}
Do your Positive first. most people like this better than all the negatives.
Side Note: for code golfing you just shaved 2 characters as well as made it more standard compliant if this nitpick can be considered a standard.
Short Version:
If you know Ternary operators and would like to use them instead of this simple if statement, @renatargh mentioned that you could make this super short
return validChars.test(keychar) && keyChars.test(keychar) ? keychar : false;
Also, var alphanumeric = "[ A-Za-z0-9]"; is never used (in this code block) and neither is
var keyChars = /[\x00\x08]/;
you should just get rid of them
php - password mismatch alert javascript on onkeypress - Stack Overflow
javascript - on keyup event of password field validation should perform - Stack Overflow
Alphanumeric password validation in javascript - Stack Overflow
Javascript validation onkeypress function - Stack Overflow
Videos
There are a couple of libraries that you could use. If you want to stick to pure JavaScript without any jQuery, then your best option would probably be Validate JS.
There are a ton of jQuery options if you are willing to work with jQuery - these are usually more feature packed and nicer to look at too. You could also use the Validator built into the Foundation Framework - it's called Abide but it uses jQuery.
Hope this helps.
This may or may not be the answer you are looking for, but perhaps you should be looking at a solution that requires less JavaScript:
In HTML 5, you can specify the type of value that an input is supposed to accept using a pattern, you can read about this on this mozilla page or by reading the answers on this question: HTML5 Form Pattern / Validation.
<input type="text" name="country_code" pattern="put a regex here that describes only valid input for your situations" title="Three letter country code">
Note that not all browsers (primarily Safari and older IE) currently support the pattern attribute.
Another thing of note is that it may be preferable to use a RegEx in your JavaScript code, should that be the preferred solution.
Probably invalid syntax in your onChange event, I avoid using like this (within the html) as I think it is messy and it is hard enough keeping JavaScript tidy at the best of times.
I would rather register the event on the document ready event in javascript. You will also definitely want to use keyup event too if you want the validation as the user is typing:
$(document).ready(function () {
$("#txtConfirmPassword").keyup(checkPasswordMatch);
});
Here is a working example
Personally I would prefer to do the check when either password field changes, that way if they re-type the original password then you still get the same validation check:
$(document).ready(function () {
$("#txtNewPassword, #txtConfirmPassword").keyup(checkPasswordMatch);
});
Here is a working example
Here's a working jsfiddle
http://jsfiddle.net/dbwMY/
Things to note:
- validate event handler bound within the document.ready function - otherwise the inputs won't exist when the JS is loaded
- using keyup
In saying that, validation is a solved problem there are frameworks that implement this functionality.
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
I'd suggest using one of these rather than reimplementing Validation for every app you write.
You don't need some big monster single regular expression, which would probably involve arcana such as lookaheads, and be hard to maintain as new conditions were added. All you need is
function validatePassword(pw) {
return /[A-Z]/ .test(pw) &&
/[a-z]/ .test(pw) &&
/[0-9]/ .test(pw) &&
/[^A-Za-z0-9]/.test(pw) &&
pw.length > 4;
}
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(\W|_)).{5,}$
You can use the above regex with lookahead and you can easily append any other criteria if required in future. You're basically checking if each of your criteria is present by lookaheads.
if(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(\W|_)).{5,}$/.test(pwd)){
// valid password
}
DEMO
You can use a regular expression and check the length. The regular expression is a case insensitive check for one letter and one number in any order. The length must be greater than 3.
function testString(s) {
var re = /[a-z]\d|\d[a-z]/i;
return re.test(s) && s.length > 3;
}
Try this:
// returns true if the form was valid; false otherwise.
function validateForm() {
var allLetters = /^[a-zA-Z]+$/;
var letter = /[a-zA-Z]/;
var number = /[0-9]/;
var firstName = document.order_form.first_name.value;
var surname = document.order_form.surname.value;
var email = document.order_form.user_email.value;
var password = document.order_form.password.value;
var invalid = [];
if (!allLetters.test(firstName)) {
invalid.push("*First Name");
}
if (!allLetters.test(surname)) {
invalid.push("*Surname Name");
}
if (email.indexOf("@") < 1 || email.lastIndexOf(".") < email.indexOf("@") + 2 || email.lastIndexOf(".") + 2 >= email.length) {
invalid.push("*Email");
}
if (password.length < 4 || !letter.test(password) || !number.test(password)) {
invalid.push("*Password");
}
if (invalid.length != 0) {
alert("Please provide response: \n" + invalid.join("\n"));
return false;
}
return true;
}
Give it a good read, run it, and ask questions if something is unclear.
Notice the use of RegExp.test instead of String.search.
If the passwords match, it will skip your entire else block:
//confirm passwords match and have been created
if ((passForm.passInput.value) == (passForm.confirmPassInput.value)) {
alert("Your password has been created!");
} else {
//you are doing validation here.
You need to run multiple checks:
window.onload = function() {
var subButton = document.getElementById("submit");
subButton.onclick = function value(passForm) {
}
};
function value(passForm) {
/** This function is being used to find out the values input by the user in both the password and confirm password text boxes.
* The results are fed back to the user using alerts.
* **/
//check for lower case
if (!passForm.passInput.value.match(/[a-z]/)) {
alert("Password must contain at least one lower case letter.");
passForm.passInput.focus();
return false;
}
//Validating length
if ((passForm.passInput.value).length < 8) {
alert("Your password has less than 8 characters.");
passForm.passInput.focus();
return false;
}
//Validationg confirmation matches
if (passForm.confirmPassInput.value != passForm.passInput.value) {
alert("Your confirmation password does not match.");
passForm.passInput.focus();
return false;
}
//Validating confirmation input
if (passForm.confirmPassInput.value == "") {
alert("Please confirm your password.");
passForm.passInput.focus();
return false;
}
//check for upper ase
if (!passForm.passInput.value.match(/[A-Z]/)) {
alert("Password must contain at least one upper case letter.");
passForm.passInput.focus();
return false;
}
//check for number
if (!passForm.passInput.value.match(/\d+/g)) {
alert("Password must contain at least one number.");
passForm.passInput.focus();
return false;
}
//confirm passwords match and have been created
if ((passForm.passInput.value) == (passForm.confirmPassInput.value)) {
alert("Your password has been created!");
return true;
}
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/security.js"></script>
<title>E-Commerce Security Features</title>
</head>
<body>
<heading>
<h1>E-Commerce Security Practices</h1>
<p id="heading"></p>
</heading>
<h3> Welcome to the E-Commerce Security Practices page! Here you will complete a few <i>simple</i> website security procedures and then decide
what practice worked best and easiest for you. </br> Have fun!</h3>
<form name="passForm" method="post" onsubmit="return value (this)">
<p>In the section below, you are asked to create a username and a password in order to 'login'. </br> Your password <b>must</b> include 8 or more
characters, an upper and lower case letter and a number and a symbol. If your password does not include any of these requirements, it will not be accepted.</p>
Your Password: <input type="password" name="passInput" placeholder="Password" />
</br>
Confirm Password: <input type="password" name="confirmPassInput" placeholder="Re-Enter Password"/>
<input type="submit" value="Submit" id="submit" onclick="return value (passForm) ;"/>
</form>
</body>
</html>
I guess you need to use regular expression look like
(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/)
Minimum eight characters, at least one letter, one number and one special character:
"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$""^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$"
Minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}"
Minimum eight and maximum 10 characters, at least one uppercase letter, one lowercase letter, one number and one special character:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,10}"