• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 932
  • Last Modified:

Javascript for enforcing proper format of a telephone number

Hi!

I currently have a javascript that pops up a window when the user enters the telephone number in the wrong format (something other than 555-999-1212) and returns the cursor to this field after pressing the ok button. I have more than one telephone number field that this would be applied to so it would need to be a javascript function as it is now.

Is there a way to modify this javascript so that the user would automatically get the formatting and if they tried to override the formatting then they would get the pop up window?

For example, if they enter 555 (area code) then the script would append "-" then they would enter 999 (prefix) and the script would append another "-" then they would just enter the last four digits without leaving the field. So when complete it would look like 555-999-1212, but if they manually typed say 555/999/1212 or (555) 999-1212 they would get the current alert window and be returned to the this field after pressing ok.

  <script language="JavaScript">
    <!--//
    var doSubmit = true;
    function checkPhone(fld) {
    if( (fld.value != "") && (/[0-9]{3}-[0-9]{3}-[0-9]{4}/i.exec(fld.value) > -1) ) {
       doSumbit = false;
       window.alert("The phone number must be in this format: 123-456-7890.\n");
                  fld.focus();
            } else {
                  doSubmit = true;
            }
    }
    //-->
    </script>


xsl:

            <INPUT maxLength="13" name="priphone" size="13" value="" onfocus="doSubmit=false;" onblur="checkPhone(this);"/>
 
Thank you in advance.

Chuck
0
TrueBlue
Asked:
TrueBlue
  • 4
  • 2
1 Solution
 
devicCommented:
hi TrueBlue,

try this:
=======================
<html>
<head>
<script>

function helpuser()
{
      var usertel=document.f1.priphone;
      if(usertel.value.match(/[\.|\(|" "|a-z]/))
      {
            alert("The phone number must be in this format: 123-456-7890.\n");
            usertel.value=usertel.value.substring(0,usertel.value.length-1);
            usertel.focus();
            return;
      }
      var m1 =usertel.value.match(/^[0-9]{3}$/);
      var m2 =usertel.value.match(/^[0-9]{3}-[0-9]{3}$/);
      if(m2){usertel.value=m2+"-"; return}
      if(m1){usertel.value=m1+"-"; return}
}
 </script>
</head>

<body>
<form name=f1>
<INPUT maxLength="13" name="priphone" size="13" value="" onkeyup=helpuser()>
</form>

</body>
</html>
0
 
VincentPugliaCommented:
Hi TrueBlue,

The script in the following link allows you to mask the input and accepts either alpha 1-800-MATTRES (upon confirm) or numerics:

http://members.aol.com/grassblad/html/maskFone2.html

Vinny
0
 
TrueBlueAuthor Commented:
Hi Vinny!

Is there a way to make the function so that it will work for more than one field without having a function different for each telephone number? Maybe make this line have a variable?

 var usertel=document.f1.priphone;  where is says priphone

Also I notice if you enter a value then go back and accidentally remove a dash or last digit that the warning is not called.

Any ideas?

Chuck



   
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
devicCommented:
<html>
<head>
<script>

function helpuser(usertel)
{
     if(usertel.value.match(/[\.|\(|" "|a-z]/))
     {
          alert("The phone number must be in this format: 123-456-7890.\n");
          usertel.value=usertel.value.substring(0,usertel.value.length-1);
          usertel.focus();
          return;
     }
     var m1 =usertel.value.match(/^[0-9]{3}$/);
     var m2 =usertel.value.match(/^[0-9]{3}-[0-9]{3}$/);
     if(m2){usertel.value=m2+"-"; return}
     if(m1){usertel.value=m1+"-"; return}
}
 </script>
</head>

<body>
<form name=f1>
<INPUT maxLength="13" name="priphone" size="13" value="" onkeyup=helpuser(this)>
<INPUT maxLength="13" name="priphone" size="13" value="" onkeyup=helpuser(this)>
<INPUT maxLength="13" name="priphone" size="13" value="" onkeyup=helpuser(this)>
</form>

</body>
</html>
0
 
devicCommented:
>>>>Also I notice if you enter a value then go back and accidentally remove a dash or last digit that the warning is not called.

========================================
<html>
<head>
<script>
var lastusertel="";
function helpuser(usertel)
{
     if(
       (usertel.value.match(/[\.|\(|" "|a-z]/))
       ||(usertel.value.match(/^[0-9]{0,2}-/))
       ||(usertel.value.match(/^[0-9]{4,}/))
       ||(usertel.value.match(/^[0-9]{3}-[0-9]{4,}/))
       )
     {
          alert("The phone number must be in this format: 123-456-7890.\n");
          usertel.value=lastusertel;
          usertel.focus();
          return;
     }
       lastusertel=usertel.value;
     var m1 =usertel.value.match(/^[0-9]{3}$/);
     var m2 =usertel.value.match(/^[0-9]{3}-[0-9]{3}$/);
     if(m2){usertel.value=m2+"-"; lastusertel=m2+"-";return}
     if(m1){usertel.value=m1+"-"; lastusertel=m1+"-"; return}
}

 </script>
</head>

<body>
<form name=f1>
<INPUT maxLength="13" name="priphone" size="13" value="" onchange=helpuser(this) onkeyup=helpuser(this) onkeydown=helpuser(this)>
<INPUT maxLength="13" name="priphone" size="13" value="" onchange=helpuser(this) onkeyup=helpuser(this) onkeydown=helpuser(this)>
<INPUT maxLength="13" name="priphone" size="13" value="" onchange=helpuser(this) onkeyup=helpuser(this) onkeydown=helpuser(this)>
</form>

</body>
</html>
0
 
TrueBlueAuthor Commented:
Hi:

This works much better.

I still have a two scenarios:

First, If a user decides that say the second telephone number is no longer valid, they can not use the back space key and remove all numbers. It stops at the second hypen. If I use the mouse I can then delete the prefix, but as soon as I try to remove the area code I get the popup. Is there a way to allow the backspace key to remove all digits and for the popup window logic to ignore the field if it is empty?

Second: If I just try to change the area code I get the popup and the value is changed back to what it was originally.

Chuck
0
 
devicCommented:
Chuck,

new version:
=====================================
<html>
<head>
<script>
var lastusertel="";
function helpuser(usertel)
{
     var m1 =usertel.value.match(/^[0-9]{3}$/);
     var m2 =usertel.value.match(/^[0-9]{3}-[0-9]{3}$/);
     if(m2){usertel.value=m2+"-";return}
     if(m1){usertel.value=m1+"-"; return}
}
function convert(usertel)
{
       if(!usertel.value.match(/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/))
     {
          alert("The phone number must be in this format: 123-456-7890.\n");
          usertel.focus();
          return;
     }
}
 </script>
</head>

<body>
<form name=f1>
<INPUT maxLength="12" name="mobil_tel" size="12" value="" onblur=convert(this) onkeyup=helpuser(this) onkeydown=helpuser(this)>
<INPUT maxLength="12" name="home_tel" size="12" value="" onblur=convert(this) onkeyup=helpuser(this) onkeydown=helpuser(this)>
<INPUT maxLength="12" name="office_tel" size="12" value="" onblur=convert(this) onkeyup=helpuser(this) onkeydown=helpuser(this)>
</form>

</body>
</html>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now