JQuery MVC - Masked Textbox,接受字母和数字

时间:2016-01-27 20:03:25

标签: c# jquery asp.net-mvc maskedtextbox

所以,我有我的常规文本框,但我似乎找不到接受字母数字值的掩码。

HTML:

<div class="form-group">                   
    <div class="col-md-6">
        <label class="control-label">Security code:</label>      
        @Html.TextBox("txtSecCode", null, new { @class = "form-control", maxlength = 20 })
    </div>                    
</div>

Javascript:(顺便说一句,我正在使用razer引擎视图)

<script type="text/javascript">
    $(function () {
        $('#txtSecCode').keyup(function () {
            // I used this to not let the user input any special characters.
            if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
                this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
            }
        });

        // The 'blur' function is used to auto-mask the field when it's not focused
        $('#txtSecCode').blur(function () {
            // So that's my mask, at first my field's maxlength was set to 
            // 16, but than some errors occured and then I realized that the 
            // dots(.) of the mask were the guilt for this.
            $('#txtSecCode').mask('9999.9999.9999.9999'); 
        });                                                     
    });
</script>

无论如何,掩码应该接受如下值:'98AC.981X.B8TZ.EW89'。 如果有人知道允许我输入字母数字值的文本框字段掩码,请告诉我。

提前致谢:D

1 个答案:

答案 0 :(得分:0)

此正则表达式仅匹配数字,字母和.

[a-zA-Z0-9.]

就你的一般方法而言,我认为你可以通过HTML5验证更简洁地完成同样的事情。使用pattern属性,您可以在输入元素上指定正则表达式,如下所示:

<input required pattern="[a-zA-Z0-9.]+">

我看到你在剃须刀中使用MVC助手创建你的元素......我相信你可以改变你的第4行并取出js:

@Html.TextBox("txtSecCode", null, new { @class = "form-control", maxlength = 20,  required = "required", pattern = "[a-zA-Z0-9.]+"})

required设置为什么并不重要,只需将其设置为确保属性包含在内的内容即可。

相关问题