Solved

Javascript for enforcing proper format of a telephone number

Posted on 2003-11-01
7
916 Views
Last Modified: 2008-02-01
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
Comment
Question by:TrueBlue
  • 4
  • 2
7 Comments
 
LVL 25

Expert Comment

by:devic
ID: 9664263
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
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9664307
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
 

Author Comment

by:TrueBlue
ID: 9665728
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 25

Expert Comment

by:devic
ID: 9665739
<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
 
LVL 25

Expert Comment

by:devic
ID: 9665802
>>>>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
 

Author Comment

by:TrueBlue
ID: 9666289
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
 
LVL 25

Accepted Solution

by:
devic earned 500 total points
ID: 9666423
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now