3
I put the following script to put only month and year in input, and set regex to accept typing from 1 to 12 for months:
$.mask.definitions['X'] = "^([0-1]|1[0-2])$";
jQuery(function($){
$("#masked1").mask("X/9999");
$("#masked2").mask("X/2015");
});
But when I put the X, it only allows to enter a number, so if I want to put month 12, I can’t. Someone has some solution?
Your regex shouldn’t be
"^([2-9]|1[0-2])$"
?– Victor Stafusa
@Victorstafusa The problem is the spacing, when I put only one X, it allows the typing of only one number, and if I put "XX" , not the right one, then it allows typing 22 , for example.
– Ysabelle Sousa
I don’t see how you can make it work (and I say from the usability point of view mainly). If the user has typed
1
, Did he want the month January or will he still write another digit? It would be better to require zero... As to avoid months 13 or above, unfortunately I have nothing to suggest (that question on Soen and his answers imply that there is nothing ready in the plugin to do this, being necessary to customize with own code). By the way, you are using the plugin Masked, right?– mgibsonbr
I agree that @mgibsonbr said, I believe it is not possible, but providing the regex that I believe is the most appropriate
var r = new RegExp('^0[1-9]|1[0-2]$')
– Guilherme Lautert
Right! Thank you for the tips! I will validate in another way then. And yes @mgibsonbr, I am using Masked.
– Ysabelle Sousa
Did you solve your problem? @Ysabelle Sousa
– PauloHDSousa