jQuery input mask length

佐手、 提交于 2020-01-12 14:41:08

问题


I have an input field and I want to limit it to alphanumerical (A-Z, a-z, 0-9) characters only, with a MINIMUM field length of 5, and a maximum length of up to 15 characters total.

Does anyone know how I can do this using jQuery?

I'm trying to use the jQuery input mask by digitalBush - http://digitalbush.com/projects/masked-input-plugin/

The problem is that UNLESS I enter 15 characters, the field goes blank. If I enter "012345678912345" (15 characters) in the input field then the value is stored and it's fine.

But if I enter "12345" as a username, when that input box loses focus its value goes back to being blank. Is there something that I can change in the "definitions" or options somewhere that fixes this?

Many thanks for your help :)

Tim


回答1:


This is done like this: The '*' is alphanumeric, anything after the '?' is optional.

jQuery(function($){
   $("#elem").mask("*****?**********");
});



回答2:


Also If you want to change the mask depending on character length, as I did. Found this great way:

var arr = ['HomePhone',
    'CellPhone',
    'EmergencyPhone'
    ];
  for (var i in arr)
  {
    $('input[name='+arr[i]+']').mask("999-999-9999?9")
        .keydown(function () {
          var $elem = $(this);
          var a = this.value.replace(/\D/g, "").length;
          setTimeout(function () {
            var n = $elem.val().replace(/\D/g, "").length;
            if (n !== a && (n === 10 || n === 11)) {
              var mask = (n === 11 ? "9999-999-9999" : "999-999-9999?9");
              $elem.mask(mask);
            }
          }, 1);
        });
  }


来源:https://stackoverflow.com/questions/2479653/jquery-input-mask-length

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!