Solved

Javascript for enforcing proper format of a telephone number

Posted on 2003-11-01
7
922 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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

626 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