搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

Learn More

Why Javascript is not working properly?

  • 2 个回答
  • 10 人有此问题
  • 27 次查看
  • 最后回复者为 tpb2012

more options

I'm working on a web page and trying this script but it is not working (although it is working fine in IE). The code is given below:

My function is:

function validateNumber()
        {
            var x=window.event.keyCode;
            //alert(x);
            if(!(x>=48 && x<=57))
                {
                    alert("Please type a number between 0 and 9 !!");
                }
        }


I'm using it like:

<input type="text" name="cust_age" value=0 size="15" onkeypress="return validateNumber()" style="font-size: 16pt" />

Please let me know how to resolve this problem.

I'm working on a web page and trying this script but it is not working (although it is working fine in IE). The code is given below: My function is:<br /> <br /> <pre><nowiki>function validateNumber() { var x=window.event.keyCode; //alert(x); if(!(x>=48 && x<=57)) { alert("Please type a number between 0 and 9 !!"); } }</nowiki></pre> I'm using it like:<br /> <br /> <pre><nowiki><input type="text" name="cust_age" value=0 size="15" onkeypress="return validateNumber()" style="font-size: 16pt" /></nowiki></pre> Please let me know how to resolve this problem.

由cor-el于修改

被采纳的解决方案

Firefox doesn't use a global window event. On Firefox you need to pass the event to the function.

I think that this should work (I'm not an expert):

<input type="text" name="cust_age" value=0 size="15" onkeypress="return validateNumber(event)" style="font-size: 16pt" />

function validateNumber(event)
        {
            var x=window.event? window.event.keyCode : event;
            //alert(x);
            if(!(x>=48 && x<=57))
                {
                    alert("Please type a number between 0 and 9 !!");
                }
        }
定位到答案原位置 👍 3

所有回复 (2)

more options

选择的解决方案

Firefox doesn't use a global window event. On Firefox you need to pass the event to the function.

I think that this should work (I'm not an expert):

<input type="text" name="cust_age" value=0 size="15" onkeypress="return validateNumber(event)" style="font-size: 16pt" />

function validateNumber(event)
        {
            var x=window.event? window.event.keyCode : event;
            //alert(x);
            if(!(x>=48 && x<=57))
                {
                    alert("Please type a number between 0 and 9 !!");
                }
        }
more options

Thanks cor-el, that helped! :)