2016-01-27 43 views
1

所以,我有我的常规文本框,但我似乎无法找到接受字母数字值的蒙版。JQuery MVC - 接受字母和数字的蒙面文本框

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

回答

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来,只是你把它设置的东西,以确保属性,都会包括在内。

相关问题