Solved

Telephone and Email form validation, javascript

Posted on 2013-02-04
9
333 Views
Last Modified: 2013-02-08
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();}}

Open in new window

0
Comment
Question by:Lee R Liddick Jr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 6

Expert Comment

by:manch
ID: 38853900
Include these javascript functions to your code.
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:

/* Regular Expression to validate the Phone Number format. */
        var validPhoneNumber = \b\d{3}-\d{4};

Open in new window


<script language="javascript" type="text/javascript">

    /*Javascript function validating a Text Box input for a valid E-Mail address. */
    function IsValidEMail() 
    {
        /* ToDo : Please replace the name of your EMail Control with txtEMail here.*/
        var email = document.getElementById("<%=txtEMail.ClientID %>").value;
        
        /* Regular Expression to validate the EMail address format. */
        var validEMail = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

        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;
        }
    }

    /*Javascript function validating a Text Box input for a valid Phone Number. */
    function IsValidPhoneNumber () {
        /* ToDo : Please replace the name of your PhoneNumber Control with txtPhoneNumber here.*/
        var phoneNumber = document.getElementById("<%=txtPhoneNumber.ClientID %>").value;

        /* ToDo : Regular Expression to validate the Phone Number format. */
        var validPhoneNumber = \b\d{3}-\d{4};

        var gotItCorrect = phoneNumber.match(validPhoneNumber);

        /* If the EMail is not correct */
        if (!gotItCorrect) {

            alert("Please enter a valid Phone Number.");

            /* ToDo : Please replace the name of your EMail Control with txtEMail here.*/
            document.getElementById("<%=txtPhoneNumber.ClientID %>").focus();
            return false;
        }
        else {
            return true;
        }
    }

</script>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38853948
Please use the CODE button or [ code ] tags.

code button
I have edited your posts to wrap the code in [ code] [/ code]
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38853955
manch: I suggest you do not alert in your function and instead change

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;
        }

Open in new window

to
  return email.match(validEMail);

Open in new window

and have the calling function take care of display

Also your phone number RegEx is not valid
0
Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 38854042
Here is my suggestion

regexes are from http://regexlib.com/DisplayPatterns.aspx?categoryId=7

function isValidEmail(str)  {
  // Email
   return  /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/.match(str);
}

function isValidPhone(str)  {
 // US telephone number 
   return /^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[ ]?([^0-1]){1}([0-9]){2}[ ]?[-]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$/.match(str); 
}

function doCreateUser(edit) {
   chkArray = [];
   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 == "") {
      chkArray.push("A valid 'user first name' is required");
   }
   if (chkLNm == "") {
      chkArray.push("A valid 'user last name' is required");
   }
   if (chkComPhn == "" || !isValidPhone(chkComPhn)) {
      chkArray.push("A valid 'email address' is required");
   }
   if (chkComEml == "" || !isValidEmail(chkComEml)) {
      chkArray.push("A valid 'email address' is required");
   }
   if (chkTZone == "") {
      chkArray.push("A valid 'user time zone' selection is required");
   }	  
   if (chkFacID == "") {
      chkArray.push("A valid 'facility' selection is required");
   }
   if (chkRole == "") {
      chkArray.push("A valid 'role' selection is required");
   }
   if (chkArray.length> 0) {
      var chk_txt = "Errror! The form cannot be submitted due to the following:\n";
      for (var i=0, n=chkArray.length;i<n;i++) {
         chk_txt += "\n"+(i+1) + ") " + chkArray[i];
      }
      alert(chk_txt);
      return false;
   }
  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();
  return true;
}

Open in new window

0
 

Author Comment

by:Lee R Liddick Jr
ID: 38855936
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.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38856757
I'll test in jsfiddle tomorrow
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 500 total points
ID: 38858533
Sorry, I swapped the string and the regex

DEMO

function isValidEmail(str)  {
  // Email
   return  str.match(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/);
}

function isValidPhone(str)  {
 // US telephone number 
   return str.match(/^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[ ]?([^0-1]){1}([0-9]){2}[ ]?[-]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$/);
}

Open in new window

0
 

Author Comment

by:Lee R Liddick Jr
ID: 38865861
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);}

Open in new window

0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 500 total points
ID: 38869298
if (chkComPhn != "" && !isValidPhone(chkComPhn)) {
      chkArray.push("A valid 'email address' is required");
 }
if (chkComEml != "" && !isValidEmail(chkComEml)) {
     chkArray.push("A valid 'email address' is required");
}
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question