Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript for enforcing proper format of a telephone number

Posted on 2003-11-01
7
Medium Priority
?
925 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

824 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