JQuery Masked Input plugin doesn't work

后端 未结 3 989
情书的邮戳
情书的邮戳 2021-01-13 22:40

I\'ve added a JQuery Masked Input plugin to my Web project but it\'s not working at all.

The plugin can be found here: http://digitalbush.com/projects/masked

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 23:20

    You need to wrap your jQuery in document.ready as several folks have already mentioned. You also need to adjust your mask to match your desired input. I assume you only want alpha characters allowed. This JSFiddle shows you an example with that assumption.

    If you want alphanumeric just replace 'a' with '*'. Below is the jQuery Code:

    $(function() {
       //The # of "a"s you enter depends on your max field input
       //The "?" makes any character after the first one optional
       $("#fname").mask("a?aaaaaaaaaaaaaaaaaaaaaaaa"); 
       $("#lname").mask("a?aaaaaaaaaaaaaaaaaaaaaaaa"); 
    });
    

    It should also be said that using the masked input plugin may not be the best option to validate a name as it is meant for fixed width inputs. If you want to validate alpha characters of varying lenghts consider doing something like this instead.

提交回复
热议问题