【问题标题】:Restrict comma in asp.net textbox限制asp.net文本框中的逗号
【发布时间】:2015-06-09 07:45:55
【问题描述】:

我想限制在文本框中使用逗号。最初文本框是禁用的,只有当用户点击相关复选框时,文本框才会启用。

这是我尝试过的代码:

function checkForComma(keyCode) {
    if (event.keyCode == 188) {
        alert('Not allowed');
    }
}

还有 EditorTemplate:

@Html.TextBoxFor(m => m.AddressLine1, @Model.isUsed ? 
    (object)new { @class = "form-control" } : 
    new { @class = "form-control", @disabled = "disabled", @onkeypress = "checkForComma(this);" })

@Html.ValidationMessageFor(m => m.AddressLine1)

【问题讨论】:

    标签: javascript asp.net asp.net-mvc validation


    【解决方案1】:

    您忘记在函数中放置 return false。修改你的功能如下

    function checkForComma(event) {
        if (event.charCode == 44) {
            alert('Not allowed');
            return false;
        }else{
            return true;
        }
    }
    

    同时修改你的html代码如下

    @Html.TextBoxFor(m => m.AddressLine1, @Model.isUsed ? 
        (object)new { @class = "form-control" } : 
        new { @class = "form-control", @disabled = "disabled", @onkeypress = "return checkForComma(event);" })
    

    【讨论】:

    • 您的警报信息是否显示?
    • 没有任何显示
    • 如何在文本框中允许逗号并在输入文本的末尾限制它
    • 您需要检查任何按钮单击或文本框模糊。
    • 你能举个例子吗
    猜你喜欢
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多