JavaScript validation not occurring correctly. Form submits anyway without checking the issue
I've tried changing from
getElementByID.value;
and document.form[][].value;
Tried changing my javascript to do it on my function init();
but also in HTML obsubmit attribute.
Really confused on what im doing wrong
function regValidate() {
var errMsg = document.getElementById("errMsg");
var username = document.getElementById("Username").value;
errMsg.style.padding = "10px";
var emailRE = "@";
var email = document.forms["register"]["email"].value;
if (email.match(emailRE) || email.length < 6) {
errMsg.innerHTML = "Please enter a valid email";
return false;
} else if (username.length < 5) {
errMsg.innerHTML = "Please enter a valid username";
}
}
<form name="register" action="(this is a realwebsite, removing due to privacy + legal reasons)" onsubmit="return regValidate()" method="post">
<p>
<h1 class="form--title">Register</h1>
</p>
<div id="errMsg"></div>
<p>
<label for="email">Email</label>
<input type="text" name="email" id="email" placeholder="Enter your email" onkeypress="this.style.width = ((this.value.length + 1) * 8) + 'px';" />
</p>
<p>
<label for="username">User ID</label>
<input type="text" name="username" id="username" placeholder="Enter your username" onkeypress="this.style.width = ((this.value.length + 1) * 8) + 'px';" />
</p>
<p>
<input type="submit" value="submit" /><br>
<input type="reset" value="reset" />
</p>
There is no error message it just goes straight to the form page which displays what was submitted for what name attribute...
return regValidate(e)there's no variablee, it should beevent.regValidate()function is missing theeparameter.return false;when the username validation fails.