为什么我的code不允许特定的符号? [英] Why my code doesn't allow specific symbols?

查看:133
本文介绍了为什么我的code不允许特定的符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这code,只允许数字输入文本中,但现在我想允许太。我修改此code,但无法正常工作。

 函数isNumberKeyDotAllowed(EVT){
    VAR CHAR code =(evt.which)? evt.which:event.key code
    如果(字符code> 31安培;及(字符$ C $℃下48 ||字符code> 57)及和放大器;字符code == 46)
        返回false;    返回true;
}

在标记文本框声明:

 < ASP:文本框=服务器ID =txtBoxApplicantCNICNo
    安其preSS =返回isNumberKeyDotAllowed(本)的AutoPostBack =真
    OnTextChanged =txtCHan_event的CssClass =表格控>


解决方案

我看到两个问题你code。首先,你通过这个作为参数传递给 isNumberKeyDotAllowed ,而你应该通过事件

 安其preSS =返回isNumberKeyDotAllowed(事件);

二是验证条件。下面是我自己的版本的函数。我定义的成功,而不是失败的条件的情况下,因为它是我更容易弄清楚:

 函数isNumberKeyDotAllowed(EVT){
    VAR CHAR code = evt.which? evt.which:evt.k​​ey code;
    如果(字符code == || 46(48< =字符code和;&安培;字符code< = 57)){
        返回true;
    }
    其他{
        返回false;
    }
}

I am using this code to allow only digits to type in textbox but now I want to allow . too. I modified this code but not working.

function isNumberKeyDotAllowed(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode == 46)
        return false;

    return true;
}

TextBox declaration in markup:

<asp:TextBox runat="server" ID="txtBoxApplicantCNICNo"
    onkeypress="return isNumberKeyDotAllowed(this)" AutoPostBack=True 
    OnTextChanged="txtCHan_event" CssClass="form-control">

解决方案

I see two problems with your code. The first is that you pass this as the argument to isNumberKeyDotAllowed while you should pass event:

onkeypress="return isNumberKeyDotAllowed(event);"

The second is the validation condition. Here is my own version of the function. I defined the condition for success instead of the condition for failure, because it is easier for me to figure out:

function isNumberKeyDotAllowed(evt) {
    var charCode = evt.which ? evt.which : evt.keyCode;
    if (charCode == 46 || (48 <= charCode && charCode <= 57)) {
        return true;
    }
    else {
        return false;
    }
}

这篇关于为什么我的code不允许特定的符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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