Coder_Nick Coder_Nick - 1 month ago 17
Javascript Question

If/else syntax on password verification in javascript

I'm fairly new to javascript and trying to list out the characters missing from a failed password to tell users what they need to input.

window.onload = function()
{
var info = document.getElementById("info");

var test = document.getElementById("myForm").test;

test.onclick = function(e)
{
e.preventDefault();
var pw = document.getElementById("myForm").pw.value;

var formula = /(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{6,}/;

if(formula.test(pw))
{
document.getElementById("myForm").submit();
}
else if pw.match(/\d/g) == null {
info.innerHTML = "You need a number";
console.log(pw.match(/\d/g))

} else {

info.innerHTML = "You need a number."
}

};

};


The above is just a first run through of checking if the user inputted a password with now numbers. But I keep getting an

Uncaught SyntaxError: Unexpected identifier


in chrome dev tools with the pw.match portion underlined. I've looked elsewhere online and my syntax looks correct. Where am I going wrong?

Answer

You need parentheses around the condition of your if block.

 else if (pw.match(/\d/g) == null) {
    ...