且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Javascript的电话格式在Firefox中不起作用

更新时间:2022-12-18 09:47:16

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

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