I want to learn how to check confirm password field in form using JavaScript and in my Firefox console, I get this error “ReferenceError: check is not defined”. According to this MDN web document, I didn’t define my variable and I must admit I don’t know what that means either.
Here’s my code.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<label>password :
<input name="password" id="password" type="password" onkeyup='check();' />
</label>
<br>
<label>confirm password:
<input type="password" name="confirm_password" id="confirm_password" onkeyup='check();' />
<span id='message'></span>
</label>
</body>
</html>
JavaScript:
var check = function() {
if (document.getElementById('password').value ==
document.getElementById('confirm_password').value) {
document.getElementById('message').style.color = 'green';
document.getElementById('message').innerHTML = 'matching';
} else {
document.getElementById('message').style.color = 'red';
document.getElementById('message').innerHTML = 'not matching';
}
}
I’d like to understand how I’m keep getting these error even if I copy line by line code from different Stack Overflow answers. What am I missing here?
Thanks in advance.