Lee R Liddick Jr
asked on
Telephone and Email form validation, javascript
I have a form that has two fields that I need to validate so the user can't submit if they don't enter the correct information for a telephone and email address. This is my script that the values get passed to. I'm not quite sure how to get the telephone and email address validation incorporated in this.
function doCreateUser(edit) {
var chk = 0;
chkArray = new Array();
var chkFNm = document.frmEdit.edtFNm.value;
var chkLNm = document.frmEdit.edtLNm.value;
var chkNmExt = document.frmEdit.edtNmExt.value;
var chkComPhn = document.frmEdit.edtComPhn.value;
var chkComEml = document.frmEdit.edtComEml.value;
var chkTZone = document.frmEdit.edtTZone.value;
var chkFacID = document.frmEdit.edtFacID.value;
var chkRole = document.frmEdit.edtRole.value;
if (chkFNm == "") {
chk++;
chkArray[chk] = "A valid 'user first name' is required";}
if (chkLNm == "") {
chk++;
chkArray[chk] = "A valid 'user last name' is required";}
if (chkComEml == "") {
chk++;
chkArray[chk] = "A valid 'email address' is required";}
if (chkTZone == "") {
chk++;
chkArray[chk] = "A valid 'user time zone' selection is required";}
if (chkFacID == "") {
chk++;
chkArray[chk] = "A valid 'facility' selection is required";}
if (chkRole == "") {
chk++;
chkArray[chk] = "A valid 'role' selection is required";}
if (chk > 0) {
var chk_txt = "Errror! The form cannot be submitted due to the following:\n\n";
for (c = 1; c <= chk; c++) {
chk_txt = chk_txt + c + ") " + chkArray[c] + "\n";}
alert(chk_txt);}
else {
document.frmAddUser.fldNewEdit.value = edit;
document.frmAddUser.fldNewFNm.value = chkFNm;
document.frmAddUser.fldNewLNm.value = chkLNm;
document.frmAddUser.fldNewNmExt.value = chkNmExt;
document.frmAddUser.fldNewComPhn.value = chkComPhn;
document.frmAddUser.fldNewComEml.value = chkComEml;
document.frmAddUser.fldNewTZone.value = chkTZone;
document.frmAddUser.fldNewFacID.value = chkFacID;
document.frmAddUser.fldNewRole.value = chkRole;
document.frmAddUser.cmdSubmit.click();}}
manch: I suggest you do not alert in your function and instead change
Also your phone number RegEx is not valid
var gotItCorrect = email.match(validEMail);
/* If the EMail is not correct */
if (!gotItCorrect) {
alert("Please enter a valid E-Mail (of format xxx@yyy.zzz).");
/* ToDo : Please replace the name of your EMail Control with txtEMail here.*/
document.getElementById("<%=txtEMail.ClientID %>").focus();
return false;
}
else {
return true;
}
to return email.match(validEMail);
and have the calling function take care of displayAlso your phone number RegEx is not valid
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you mplungjan, very detailed and helpful to follow. I am getting a script error though stating that TypeError: (intermediate value).match is not a function.
I'll test in jsfiddle tomorrow
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Okay, that works. But I am trying to get the Phone number to not be required but still be validated. Everything I am trying is not working. This was my last attempt...
if (chkFNm == "") {
chk++;
chkArray[chk] = "A valid 'user first name' is required";}
if (chkLNm == "") {
chk++;
chkArray[chk] = "A valid 'user last name' is required";}
if (chkComPhn == !isValidPhone(chkComPhn)) {
chk++;
chkArray[chk] = "A valid 'telephone' is required";}
if (chkComEml == "" || !isValidEmail(chkComEml)) {
chk++;
chkArray[chk] = "A valid 'email address' is required";}
if (chkTZone == "") {
chk++;
chkArray[chk] = "A valid 'user time zone' selection is required";}
if (chkFacID == "") {
chk++;
chkArray[chk] = "A valid 'facility' selection is required";}
if (chkRole == "") {
chk++;
chkArray[chk] = "A valid 'role' selection is required";}
if (chk > 0) {
var chk_txt = "Errror! The form cannot be submitted due to the following:\n\n";
for (c = 1; c <= chk; c++) {
chk_txt = chk_txt + c + ") " + chkArray[c] + "\n";}
alert(chk_txt);}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Remember : Replace the ToDo parts correctly in your code.
Also, for valid phone number format you may have to search for a correct Phone Number regular expression (here i have included expression for a 3-4 number format).
Suppose your web page collects a customer's seven-digit phone number and you want to verify that the phone number is in the correct format, "xxx-xxxx", where each "x" is a digit. The following expression will search through text looking for such a string:
Open in new window
Open in new window