Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am using the following function for auto format of phone numbers. It is working good in other browsers except Firefox. Please can any body tell me why it is not working in Firefox.

function FormatPhone()
       {

           if( ((window.event.keyCode >= 97) && (window.event.keyCode <= 122)) || ((window.event.keyCode >= 65) && (window.event.keyCode <= 90)) )
           {
               return false;
           }
           else
           {
               if (event.keyCode == 8 || event.keyCode==37)
               {
                   return;
               }

               phone = event.srcElement.value;
               if (phone.length==1 && phone != "(")
               {
                   event.srcElement.value = "(" + phone;
               }
               else if (phone.length==4 && phone.substring(0,1) == "(" && ! isNaN(phone.substring(1,4)))
               {
                   event.srcElement.value = phone + ") ";
               }
               else if (phone.length==9 && phone.substring(0,1) == "(" && ! isNaN(phone.substring(1,4)) && phone.substring(4,5) == ")" && ! isNaN(phone.substring(6,9)))
               {
                   event.srcElement.value = phone + "-";
               }
           }
       }


I am calling this function in the page_load
txtPhone2.Attributes.Add("Onkeypress", "return FormatPhone()");


Thank you very much,
Nag
Posted
Updated 10-Sep-12 9:56am
v2

1 solution

I have solved this problem with the below code...

function FormatPhone(e) {
            evt = e || window.event;
            var keyPressed = evt.which || evt.keyCode;

            if(((keyPressed >= 97) && (keyPressed <= 122)) || ((keyPressed >= 65) && (keyPressed <= 90))) {
                return false;
            }
            else {
                if (keyPressed == 8 || keyPressed == 37) {
                    return;
                }

                var target = evt.target || evt.srcElement;

                phone = target.value;
                if (phone.length == 1 && phone != "(") {
                    target.value = "(" + phone;
                }
                else if (phone.length == 4 && phone.substring(0, 1) == "(" && !isNaN(phone.substring(1, 4))) {
                    target.value = phone + ") ";
                }
                else if (phone.length == 9 && phone.substring(0, 1) == "(" && !isNaN(phone.substring(1, 4)) && phone.substring(4, 5) == ")" && !isNaN(phone.substring(6, 9))) {
                    target.value = phone + "-";
                }
            }
        }


and passing the event in the page_load
txtPhone2.Attributes.Add("Onkeypress", "return FormatPhone(event)");


Thanks,
Nag
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900