Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
Can anyone help me with the password validation using JavaScript?

1 Answer

0 votes
by (26.7k points)

You can use this below code to achieve 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 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;

}

I hope this will help.

Want to become a Java Expert? Join Java Course now!!

Want to know more about Java? Watch this video on Java Course | Java Tutorial for Beginners :

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...