使用Javascript的电话格式在Firefox中不起作用 [英] Phone Format using Javascript is not working in Firefox

查看:77
本文介绍了使用Javascript的电话格式在Firefox中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下功能来自动设置电话号码格式.它在除Firefox之外的其他浏览器中运行良好.请任何人告诉我为什么它不能在Firefox中运行.

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 + "-";
               }
           }
       }



我在page_load中调用此函数



I am calling this function in the page_load

txtPhone2.Attributes.Add("Onkeypress", "return FormatPhone()");



非常感谢,
Nag



Thank you very much,
Nag

推荐答案

我已经使用以下代码解决了这个问题...

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 + "-";
                }
            }
        }



并在page_load



and passing the event in the page_load

txtPhone2.Attributes.Add("Onkeypress", "return FormatPhone(event)");


中传递事件
谢谢,
Nag



Thanks,
Nag


这篇关于使用Javascript的电话格式在Firefox中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆