Hi,
I’ve written this to validate the email, but’s not working.
function main(){
document.getElementById("contactform").onsubmit = validate;
document.getElementById("email").onblur = validateEmail;
}
function validate() {
return false;
}
function requiredField() () {
}
function validateEmail(){
var email = document.getElementById("email");
var emailRegEx = /^[\\w\\.\\-]+@([\\w\\-]+\\.)+[a-zA-Z]+$/;
if (!emailRegEx.test(email.value)) {
var errorEmail = document.getElementById("errorEmail");
errorMail.innerHTML = "* Please write a valid email. "
return false;
} else {
return true;
}
}
}
function validate zhaNumber() {
return result;
}
function focusName(){
}
function defaultText(){
}
function wipePlaceholder(){
}
window.onload = main;
Here’s the form:
<body>
<h1>Contact Form</h1>
<!-- Turn HTML5 validation off to test Javascript data validation -->
<form id="contactform" action="#" method="post" novalidate>
<p><i>Please complete the form. Mandatory fields are marked with a </i><em>*</em></p>
<!-- Patient's name -->
<fieldset>
<legend>Patient's Name</legend>
<p><label for="name">First Name <em>*</em></label>
<input id="name" required>
<span id = "nameHint" class="errorMessage"></span></p>
<p><label for="surname">Last Name <em>*</em></label>
<input id="surname" required>
<span id = "surnameHint" class="errorMessage"></span></p>
<p><label for="title">Title <em>*</em></label>
<select id="title" required>
<option value="">Please select</option>
<option value="mr">Mr.</option>
<option value="ms">Ms.</option>
<option value="mrs">Mrs.</option>
<option value="miss">Miss.</option>
<option value="master">Master.</option>
</select><br>
<span id = "titleHint" class="errorMessage"></span></p>
</fieldset>
<!-- Contact details -->
<fieldset>
<legend>Contact Details</legend>
<p><label for="telephone">Telephone</label>
<input id="telephone">
<span id = "telephoneHint" class="errorMessage"></span></p>
<p><label for="email">Email <em>*</em></label>
<input id="email" type="email" required>
<span id = "errorEmail" class="errorMessage">* Please enter a valid email</span></p>
<!-- Health Number -->
<fieldset>
<legend>Health Information</legend>
<p><label for="health-number">Health Authority Number <em>*</em></label>
<input id="health-number" type="alphanumeric" min="0" max="120" step="0.1" required>
<span id = "healthNumberHint" class="errorMessage"></span></p>
</fieldset>
<p><input type="submit" value="Submit" id=submit></p>
</form>
</body>
And in the CSS I have:
.errorMessage {
color: red;
font-size: 15px;
padding-left: 1em;
display: none;
}
Here’s the demo:
I’m learning Javascript and I appreciate your help.
Thanks.