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
TrueBlueAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.