Hi everyone…I am new to javascript.
The javascript form validation works fine , but for the first name and the last name field, it is accepting numeric values , but I want it to accept only alphabets. Could you please let me know whats wrong in the code below. Also is there any other error in the below code.
–javascript file:-
// JavaScript Document
var strErrors = “”;
function validateForm() {
// clean out the string of errors if validation fails more than once
strErrors = “”;
// calls to the various function to do their particular tasks
validateName1();
validateName2();
validateEmailAddress();
validatePhone();
validateRadios();
validateCheckBoxs();
validateguestNo();
if (strErrors.length > 0) {
displayErrors();
return false;
}
else {
return true;
}
}
function validateName1() {
// get the value of what was erntered into the nrobame field
var strName = document.getElementById(“fName”).value;
if(strName.length < 2) {
strErrors += "Please enter your name.~";
}
}
function validateName2() {
// get the value of what was erntered into the nrobame field
var strName = document.getElementById(“lName”).value;
if(strName.length < 2) {
strErrors += "Please enter your name.~";
}
}
function validateEmailAddress() {
// get the value of what was erntered into the email field
var strEmail = document.getElementById(“emailAdd”).value;
var emailRegex = /^[\\w\\.]+@[a-zA-Z_]+?\\.[a-zA-Z\\.]{2,6}$/;
// check the validity of the email address now then cos something has been entered
// into the email field - already checked by the checkIfFieldEmpty() function
if (!emailRegex.test(strEmail)) {
strErrors += "Please enter a valid email address.~";
}
}
function validatePhone() {
// get the value of what was erntered into the nrobame field
var strName = document.getElementById(“phoneNo”);
var stripped = strName.value.replace(/[\(\)\.\-\ ]/g, ‘’);
if(isNaN(parseInt(stripped))) {
strErrors += "Please enter your phone number.~";
}
}
function validateRadios() {
var chks = document.getElementsByName("contactMeth");
var passed = false;
for(i = 0; i < chks.length; i++) {
if(chks[i].checked == true) {
passed = true;
}
}
if(passed == false) {
strErrors += "Please select contact method.~";
}
}
function validateCheckBoxs() {
var chks = document.getElementsByName("roomtype[]");
var passed = false;
for(i = 0; i < chks.length; i++) {
if(chks[i].checked == true) {
passed = true;
}
}
if(passed == false) {
strErrors += "Please select room type.~";
}
}
function validateguestNo() {
// get the value of what was erntered into the nrobame field
var strName = document.getElementById(“guestNo”).value;
if(strName.length < 1) {
strErrors += "Please enter number of guests.~";
}
}
function displayErrors() {
document.getElementById("errors").innerHTML = "<p>Oops! Please correct the following items:</p><ul>";
var arErrors = strErrors.split("~");
for (c = 0; c < arErrors.length - 1; c++) {
document.getElementById("errors").innerHTML += "<li>" + arErrors[c] + "</li>";
}
document.getElementById("errors").innerHTML += "</ul>";
}
Thank you…