如何使字符在Primefaces InputMask中不可编辑 [英] How to make characters non editable in primefaces inputmask

查看:42
本文介绍了如何使字符在Primefaces InputMask中不可编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前两个字符应不可编辑.

First two characters should be non editable.

示例:输入字符串的长度必须为4:文本框"TEXT"

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

用户只能更改"XT",不能更改"TE";

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

TEXT可以是AAAA,aaaa,aaa90、9a09,字符串可以是任何东西.

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.

我们可以使用p:inputtext或p:inputmask

We can use p:inputtext or p:inputmask

谢谢

推荐答案

如果您的文字可以是任何东西,并且需要固定前2个字符,那么您就没有其他选择可以使蒙版动态化并以其为基础在变量的值上.

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}" />

然后在getter的bean中(或在调用值的setter之后偷懒),将后两个字符替换为'*'

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**

如果现有定义(a,9和*)在您遇到的情况下(如它们所做的那样)阻止了您,则可以使用

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']= "";

,如果'*'的定义变窄(默认值:[a-z | A-Z | 0-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;

您也可以创建自己的定义.

请记住,这是一个客户端掩码.没有服务器端验证.如果用户篡改了允许的掩码或输入值,则服务器将不会阻止它.不知道是否应该这样做,但目前还不知道!

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

这篇关于如何使字符在Primefaces InputMask中不可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆