By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,036 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,036 IT Pros & Developers. It's quick & easy.

Auto Format Phone Number

P: 3
I used the following script to auto format the phone number field in my form.

http://javascript.internet.com/forms...ne-number.html

The problem with this code is im able to set the auto-format for only one field in the form. Please help me as to how can i make multiple phone fields have this format using this code.

Thanks.
Jul 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: 2
I used the following script to auto format the phone number field in my form.

http://javascript.internet.com/forms...ne-number.html

The problem with this code is im able to set the auto-format for only one field in the form. Please help me as to how can i make multiple phone fields have this format using this code.

Thanks.

Hello jeswin12 ,

Even i need the answer for the second question you asked i.e how to enter mutiple phone number fields in a single textbox.Did you get the solution for that?.If so , please mail me the code for entering multiple phone numbers in a textbox.


Salivendala
Oct 9 '06 #2

P: 2
Hello jeswin12 ,

I tried and i got the solution for entering multiple phone numbers in a single textbox.


<script language="javascript">

Expand|Select|Wrap|Line Numbers
  1.             function test(key,id)
  2.             {
  3.             if(key>47&&key<58)
  4.             {
  5.  
  6.             var v= document.getElementById("txtTest").value;
  7.             var m= document.getElementById("txtTest");
  8.             var z="";
  9.             var y;
  10.             var pp;
  11.             var result1;
  12.             var result2;
  13.             var o1;
  14.             if(v != "")
  15.             {
  16.                 if(v.length > 15 ) 
  17.                     {
  18.                         var b=v.split(",");                        
  19.                         //o1=document.getElementById("txtTest").value.substring(0,12) + ","  ;
  20.                         o1='';
  21.                         for(i=0;i< b.length;i++)
  22.                             {
  23.                                 o=ValidatePhones(b[i]);
  24.                                 o1=o1   + o;  
  25.                             }                             
  26.                         document.getElementById("txtTest").value =  o1 ;                            
  27.                     }
  28.  
  29.                 if(v.length > 1)
  30.                     {
  31.                         y=ValidatePhone(m);
  32.                         document.getElementById("txtTest").value=  y;     
  33.                     }
  34.                 }
  35.               }
  36.               else
  37.               {
  38.                 event.keyCode=false;                
  39.               }
  40.  
  41.         }
  42.         function ValidatePhone(m)
  43.          {
  44.             p=m.value; 
  45.             if(p.length==3)
  46.             {
  47.                 pp=p;
  48.                 d4=p.indexOf('(')
  49.                 d5=p.indexOf(')')
  50.                 if(d4==-1)
  51.                 {    
  52.                             pp="("+pp;
  53.                     }
  54.                     if(d5==-1)
  55.                     {
  56.                             pp=pp+")";
  57.                 }
  58.                 m.value="";
  59.                 m.value=pp;
  60.  
  61.             }
  62.             if(p.length>3)
  63.             {
  64.                 d1=p.indexOf('(')
  65.                 d2=p.indexOf(')')
  66.                 if (d2==-1)
  67.                 {
  68.                             l30=p.length;
  69.                             p30=p.substring(0,4);
  70.                             p30=p30+")"
  71.                             p31=p.substring(4,l30);
  72.                             pp=p30+p31;
  73.                             m.value="";
  74.                     m.value=pp;
  75.                 }
  76.             }
  77.             if(p.length>5)
  78.             {
  79.                 p11=p.substring(d1+1,d2);
  80.                 if(p11.length>3)
  81.                 {
  82.                     p12=p11;
  83.                     l12=p12.length;
  84.                     l15=p.length
  85.                     p13=p11.substring(0,3);
  86.                     p14=p11.substring(3,l12);
  87.                     p15=p.substring(d2+1,l15);
  88.                     m.value="";
  89.                     pp="("+p13+")"+p14+p15;
  90.                     m.value=pp;
  91.                 }
  92.                 l16=p.length;
  93.                 p16=p.substring(d2+1,l16);
  94.                 l17=p16.length;
  95.                 if(l17>3&&p16.indexOf('-')==-1)
  96.                 {
  97.                             p17=p.substring(d2+1,d2+4);
  98.                             p18=p.substring(d2+4,l16);
  99.                         p19=p.substring(0,d2+1);
  100.                     pp=p19+p17+"-"+p18;
  101.                     m.value="";
  102.                     m.value=pp;
  103.                 }
  104.  
  105.             }
  106.  
  107.             if(p.length==13)
  108.             {
  109.                 m.value=m.value+","; 
  110.             }
  111.             return m.value;
  112.         }
  113.  
  114.         function ValidatePhones(s)
  115.             {
  116.  
  117.             p=s;                    
  118.  
  119.             if(p.length==3)
  120.             {
  121.                 pp=p;
  122.                 d4=p.indexOf('(')
  123.                 d5=p.indexOf(')')
  124.                 if(d4==-1)
  125.                 {    
  126.                             pp="("+pp;
  127.                     }
  128.  
  129.                     if(d5==-1)
  130.                     {
  131.                             pp=pp+")";
  132.                 }
  133.                 p="";
  134.                 p=pp;
  135.             }
  136.  
  137.             if(p.length>3)
  138.             {
  139.                 d1=p.indexOf('(')
  140.                 d2=p.indexOf(')')
  141.                 if (d2==-1)
  142.                 {
  143.                             l30=p.length;
  144.                             p30=p.substring(0,4);
  145.                             p30=p30+")"
  146.                             p31=p.substring(4,l30);
  147.                             pp=p30+p31;
  148.                             p="";
  149.                                p=pp;
  150.                 }
  151.             }
  152.             if(p.length>5)
  153.             {
  154.                 p11=p.substring(d1+1,d2);
  155.                 if(p11.length>3)
  156.                 {
  157.                     p12=p11;
  158.                     l12=p12.length;
  159.                     l15=p.length
  160.                     p13=p11.substring(0,3);
  161.                     p14=p11.substring(3,l12);
  162.                     p15=p.substring(d2+1,l15);
  163.                     p="";
  164.                     pp="("+p13+")"+p14+p15;
  165.                     p=pp;
  166.  
  167.                 }
  168.                 l16=p.length;
  169.                 p16=p.substring(d2+1,l16);
  170.                 l17=p16.length;
  171.  
  172.                 if(l17>3&&p16.indexOf('-')==-1)
  173.                 {
  174.                             p17=p.substring(d2+1,d2+4);
  175.                             p18=p.substring(d2+4,l16);
  176.                         p19=p.substring(0,d2+1);
  177.                     pp=p19+p17+"-"+p18;    
  178.                     p=pp;                
  179.                 }
  180.             }
  181.  
  182.             if(p.length==13)
  183.             {
  184.                 p = p +","; 
  185.             }
  186.  
  187.             return p;            
  188.  
  189.         }
  190.  
</script>



In the html page define the textbox as
<input onkeypress="test(event.keyCode,'txtTest');" id="txtTest" type="text" size="109" runat="server">

Try this u will get it jeswin12.
Oct 10 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.