Update 11/07/2019
Yes, since the time I wrote the answer several javascript libraries have been created for mask manipulation.
I recently used a library called Imask to develop a project of a selection process, you can check the complete implementation with Vue in that repository, but I leave below an explanatory part of the implementation:
const moneyPattern = {
mask: '$num', // cria um padrão que inclui $ e o bloco num
blocks: {
num: { // define o bloco num
mask: Number, // define o padrão de Number para o bloco num
thousandsSeparator: ',' // define a separação de milhares com virgula
}
}
}
I believe that the mask will help both web and mobile development, if there is any specific need leave a comment below that I will try to increase the response.
Original Reply 13/10/2015:
Yes it is possible if you make your own mask plugin with your own needs.
I’ve tried several and I don’t know of any that works 100% in all browsers. You can even try the Jquerymask, it is a plugin very simple to tinker and well documented, but it did not meet 100% of my needs and may not meet your.
An alternative solution that I found without masks is to describe in the placeholder of the element as the user must fill and complement the feedbacks with attributes of the HTML itself as for example the Pattern HTML5, maxlength, required and others, depending on the need.
Ex.:
<label>CNPJ: *
<input name="cnpj" type="text" placeholder="00.000.000/0000-00" maxlength="18" required pattern="[0-9]{2}.[0-9]{3}.[0-9]{3}\/[0-9]{4}-[0-9]{2}" value="">
</label>
The advantage of doing it that way is that no need to load any additional plugin, where, the Pattern is responsible for validating regular expressions and give feedback to the user in real time along with the other attributes (you can also use regular expressions with Javascript for greater compatibility). Pattern stand: Can I use.
And finally I validate the data on the server side, because only one will mask in the client side is totally insecure.
For this I use the PHP preg_match. Example:
if (preg_match("/([a-zA-Z0-9])/", $email)){return true;}
This will only accept letter and number characters.
We could create an open repository for the community and start working on a plugin that meets everyone’s needs, because I believe it is still missing today...
Really mask is complicated, to be able to access the meutim I have to copy and paste the number of another place, because the parentheses do not enter as I type.
– Luis Henrique
Have you tried using this https://igorescobar.github.io/jQuery-Mask-Plugin/? I did a test and works normally on my Android running Google Chrome.
– alvarodms
Typing time mask is hell, including for the user. I hate mask as a developer and even more as a user. Try, for example, typing
123756
in a mask0#.###,00
, getting1.237,56
and select7
and correct to4
. Most masks prevent the selection of texts and play typing to the end of the value. The current recommendation is not to use masks and only to inform the user which format to use, such asdd/MM/yyyy
or30/01/1990
.– MFedatto