Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
151
rated 0 times [  156] [ 5]  / answers: 1 / hits: 37837  / 11 Years ago, mon, april 22, 2013, 12:00:00

Can anyone suggest an easy password validation using javascript. I need it to be at least 4 characters long and contain min 1 letter and 1 number.



This is a school project so its really only a simple validation needed to make sure they are entering in the required characters.



I am using



<textarea id=special_info name=address onfocus=this.value='' rows=5 cols=40 >Please enter your address...
</textarea>
<div id=maxcar>(Maximum characters: 200)
</div>


My javascript is in this format



function validate_form(){

valid=true;

var letters=/^[a-zA-Z]+$/;
var numbers=/^[0-9]+$/;
var email=document.order_form.user_email.value;
var invalid = [];

if (document.order_form.first_name.value.search(letters)==-1){
invalid.push(*First Name)
}

if (document.order_form.surname.value.search(letters)==-1){
invalid.push(*Surname Name)
}

if (email.indexOf(@)<1 || email.lastIndexOf(.)<email.indexOf(@)+2
|| email.lastIndexOf(.)+2>=email.length){
invalid.push(*Email)
}

if (invalid.length != 0)
{alert(Please provide response: n + invalid.join(n) );
valid = false;
invalid = [];}


return valid;}

More From » validation

 Answers
9

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.


[#78737] Friday, April 19, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jamir

Total Points: 736
Total Questions: 97
Total Answers: 101

Location: Cayman Islands
Member since Fri, Mar 4, 2022
2 Years ago
jamir questions
;