How to make characters non editable in primefaces inputmask

徘徊边缘 提交于 2019-12-24 16:10:03

问题


First two characters should be non editable.

Example: input string length has to be 4: text box "TEXT"

User can change only "XT" not "TE";

Edited: TEXT can AAAA, aaaa, aa90, 9a09, String can be anything.

User can change only last two character and first two character could be anything.

We can use p:inputtext or p:inputmask

Thanks


回答1:


If your text can be anything and the first 2 characters need to be fixed, you don't have any other option then to make your mask dynamic and base it on the value of the variable.

<p:inputMask value="#{bean.value}" mask="#{bean.mask}" />

And in your bean in the getter (or lazy after the setter of the value is called), replace the last two characters with a '*'

value | mask
----- | ----
 TEXT | TE**
 AAAA | AA**
 aaaa | aa**
 aa90 | aa**
 9a09 | 9a**

And if the existing definitions (a, 9 and *) are blocking things in your case (as they seem to do), you can remove them from the definitions by using

$.mask.definitions['a']= "";
$.mask.definitions['9']= "";

and if the definition of the '*' is to narrow (default: [a-z|A-Z|0-9]), you can change it to anything you like by using regular expressions:

$.mask.definitions['*']= "[\\S]"

This e.g. meaning any non-whitespace character, so #, % etc are allowed to;

You can create your own definitions too.

Security note:

Keep in mind, this is a client-side mask. There is no server-side validation to it. If a user tampers with the allowed mask or value of the input, the server will not block it. No idea if it should, but for the moment it does not!

Note to self: Never used this component, but again the PrimeFaces sourcecode, the jquery-mask-plugin and the documentation of the mask library helped here... The source is your friend



来源:https://stackoverflow.com/questions/35843128/how-to-make-characters-non-editable-in-primefaces-inputmask

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