A Stack Overflow coder said my code is too rigid and inflexible. Would someone critique my form validation code, and give some pointers on being more efficient?
Code Logic:
If the field has an error (empty or a RegEx fail) with either on submit or on change, the user will see a suitable message indicating the error and a Unicode arrow pointing to the invalid field.
If the the field has passed, the arrow and error message are not displayed.
Link to JsFiddle Code Sample
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>form validation</title>
<style type="text/css">
body, h1, input, div {
font-family: arial;
}
input[type="text"],
input[type="password"] {
font-size: 1.15em;
padding: 0.4em 0.5em;
}
/* Start red arrow error messages below input fields */
.error-text-login{
color: #ea4040;
vertical-align: bottom;
text-align: middle;
width: 315px;
display: none;
}
.error-text-login:before {
content: '\0020 \21E1 \0020';
vertical-align: bottom;
}
.error-text-login {
font-size: 0.75em;
}
.error-text-login:before {
font-size: 1.3em;
}
/* End red arrow error messages below input fields */
#usernameLogin.error input,
#passwordLogin.error input {
background-color: #fff7f7;
border: 1px solid #f68c8c;
border-radius: 2px;
box-shadow: 0 0 6px #ffeded inset;
color: #e10000;
}
#usernameLogin.default input,
#passwordLogin.default input {
background-color: #f7f7f5;
border: 1px solid #c1c1c1;
border-radius: 2px;
box-shadow: 0 0 6px #eeeeee inset;
color: #a3a3a3;
}
</style>
</head>
<body id="top">
<div class="box" style="float: left; width: 47%; margin-right: 6%; height: 200px;">
<h1>Login</h1>
<form id="theForm" method="post" action="index.html" >
<p id="usernameLogin" class="default">
<input type="text" placeholder="Username/Email" id="userName" name="userName" >
<div id="name-error" class="error-text-login"></div>
</p>
<p id="passwordLogin" class="default">
<input type="password" name="pswd" id="pswd" placeholder="Password" >
<div id="pswd-error" class="error-text-login"></div>
<input type="checkbox" name="cbMask" id="cbMask" class="css-checkbox" />
<label for="cbMask" class="css-label radGroup1">
View password? <span id="masking">NO</span>
</label>
</p>
<input type="submit" class="button wide" value="Sign In">
</form>
</div>
<script>
var form = document.getElementById("theForm");
form.onsubmit = function()
{
var errorQ = "some errors were found: \n \n";
var same = errorQ;
var userName = document.forms[0].userName.value;
var lenUserName = userName.length;
var pswd = document.forms[0].pswd.value;
var lenPswd = pswd.length;
if (userName == "" || userName == null || lenUserName <= 0){
document.getElementById('usernameLogin').className = 'error';
document.getElementById('name-error').innerHTML = "Username is required";
document.getElementById('name-error').style.display = "block";
errorQ += "password cannot be empty \n";
}
else if (userName != "" || lenUserName > 0){
document.getElementById('usernameLogin').className = 'default';
document.getElementById('name-error').innerHTML = "";
document.getElementById('name-error').style.display = "none";
}
if (pswd == "" || pswd == null || pswd <= 0){
document.getElementById('passwordLogin').className = 'error';
document.getElementById('pswd-error').innerHTML = "Password is required";
document.getElementById('pswd-error').style.display = "block";
errorQ += "password cannot be empty \n";
}
else if (pswd != "" || lenPswd.length > 0){
document.getElementById('passwordLogin').className = 'default';
document.getElementById('pswd-error').innerHTML = "";
document.getElementById('pswd-error').style.display = "none";
}
if (errorQ == same){
return true;
}
else {
//alert("start over " + errorQ);
return false;
}
return false;
}
form.onchange = function() {
var errorQ = "some errors were found: \n \n";
var same = errorQ;
var userName = document.forms[0].userName.value;
var lenUserName = userName.length;
var pswd = document.forms[0].pswd.value;
var lenPswd = pswd.length;
if (userName == "" || userName == null || lenUserName <= 0){
document.getElementById('usernameLogin').className = 'error';
document.getElementById('name-error').innerHTML = "Username is required";
document.getElementById('name-error').style.display = "block";
errorQ += "password cannot be empty \n";
}
else if (userName != "" || lenUserName > 0){
document.getElementById('usernameLogin').className = 'default';
document.getElementById('name-error').innerHTML = "";
document.getElementById('name-error').style.display = "none";
}
if (pswd == "" || pswd == null || pswd <= 0){
document.getElementById('passwordLogin').className = 'error';
document.getElementById('pswd-error').innerHTML = "Password is required";
document.getElementById('pswd-error').style.display = "block";
errorQ += "password cannot be empty \n";
}
else if (pswd != "" || lenPswd.length > 0){
document.getElementById('passwordLogin').className = 'default';
document.getElementById('pswd-error').innerHTML = "";
document.getElementById('pswd-error').style.display = "none";
}
if (errorQ == same){
return true;
}
else {
//alert("start over " + errorQ);
return false;
}
return false;
}
</script>
</body>
</html>