KeyFilter can be used to filter keyboard input on specified input components.
Documentationpint | /[\d]/ |
int | /[\d\-]/ |
pnum | /[\d\.]/ |
money | /[\d\.\s,] |
num | /[\d\-\.]/ |
hex | /[0-9a-f]/i |
/[a-z0-9_\.\-@]/i | |
alpha | /[a-z_]/i |
alphanum | /[a-z0-9_]/i |
Predefined masks and their regular expressions: <table> <tr> <td><strong>pint</strong></td> <td>/[\d]/</td> </tr> <tr> <td><strong>int</strong></td> <td>/[\d\-]/</td> </tr> <tr> <td><strong>pnum</strong></td> <td>/[\d\.]/</td> </tr> <tr> <td><strong>money</strong></td> <td>/[\d\.\s,]</td> </tr> <tr> <td><strong>num</strong></td> <td>/[\d\-\.]/</td> </tr> <tr> <td><strong>hex</strong></td> <td>/[0-9a-f]/i</td> </tr> <tr> <td><strong>email</strong></td> <td>/[a-z0-9_\.\-@]/i</td> </tr> <tr> <td><strong>alpha</strong></td> <td>/[a-z_]/i</td> </tr> <tr> <td><strong>alphanum</strong></td> <td>/[a-z0-9_]/i</td> </tr> </table> <br /> <br /> <h:form> <h:panelGrid columns="2"> <h:outputText value="KeyFilter with regEx on a p:inputText" /> <p:inputText id="text1"> <p:keyFilter regEx="/[ABC]/i"/> </p:inputText> <h:outputText value="KeyFilter with mask on a h:inputText"/> <h:inputText id="text2" /> <h:outputText value="KeyFilter with testFunction on a p:autoComplete" /> <p:autoComplete id="autoComplete1" value="#{autoCompleteView.txt1}" completeMethod="#{autoCompleteView.completeText}" /> </h:panelGrid> <p:keyFilter for="text2" mask="num"/> <p:keyFilter for="autoComplete1" testFunction="return c == 'z';"/> </h:form>
@Named @RequestScoped public class AutoCompleteView { private String txt1; private String txt2; private String txt3; private String txt4; private String txt5; private String txt6; private String txt7; private String txt8; private Theme theme1; private Theme theme2; private Theme theme3; private Theme theme4; private List<Theme> selectedThemes; @Inject private ThemeService service; public List<String> completeText(String query) { List<String> results = new ArrayList<>(); for(int i = 0; i < 10; i++) { results.add(query + i); } return results; } public List<Theme> completeTheme(String query) { String queryLowerCase = query.toLowerCase(); List<Theme> allThemes = service.getThemes(); return allThemes.stream().filter(t -> t.getName().toLowerCase().startsWith(queryLowerCase)).collect(Collectors.toList()); } public List<Theme> completeThemeContains(String query) { String queryLowerCase = query.toLowerCase(); List<Theme> allThemes = service.getThemes(); return allThemes.stream().filter(t -> t.getName().toLowerCase().contains(queryLowerCase)).collect(Collectors.toList()); } public void onItemSelect(SelectEvent<String> event) { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Item Selected", event.getObject())); } public String getTxt1() { return txt1; } public void setTxt1(String txt1) { this.txt1 = txt1; } public String getTxt2() { return txt2; } public void setTxt2(String txt2) { this.txt2 = txt2; } public String getTxt3() { return txt3; } public void setTxt3(String txt3) { this.txt3 = txt3; } public String getTxt4() { return txt4; } public void setTxt4(String txt4) { this.txt4 = txt4; } public String getTxt5() { return txt5; } public void setTxt5(String txt5) { this.txt5 = txt5; } public String getTxt6() { return txt6; } public void setTxt6(String txt6) { this.txt6 = txt6; } public String getTxt7() { return txt7; } public void setTxt7(String txt7) { this.txt7 = txt7; } public String getTxt8() { return txt8; } public void setTxt8(String txt8) { this.txt8 = txt8; } public Theme getTheme1() { return theme1; } public void setTheme1(Theme theme1) { this.theme1 = theme1; } public Theme getTheme2() { return theme2; } public void setTheme2(Theme theme2) { this.theme2 = theme2; } public Theme getTheme3() { return theme3; } public void setTheme3(Theme theme3) { this.theme3 = theme3; } public Theme getTheme4() { return theme4; } public void setTheme4(Theme theme4) { this.theme4 = theme4; } public List<Theme> getSelectedThemes() { return selectedThemes; } public void setSelectedThemes(List<Theme> selectedThemes) { this.selectedThemes = selectedThemes; } public void setService(ThemeService service) { this.service = service; } public char getThemeGroup(Theme theme) { return theme.getDisplayName().charAt(0); } }
public class Theme { private int id; private String displayName; private String name; public Theme() {} public Theme(int id, String displayName, String name) { this.id = id; this.displayName = displayName; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDisplayName() { return displayName; } public void setDisplayName(String displayName) { this.displayName = displayName; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return name; } }
@Named @ApplicationScoped public class ThemeService { private List<Theme> themes; @PostConstruct public void init() { themes = new ArrayList<>(); themes.add(new Theme(0, "Nova-Light", "nova-light")); themes.add(new Theme(1, "Nova-Dark", "nova-dark")); themes.add(new Theme(2, "Nova-Colored", "nova-colored")); themes.add(new Theme(3, "Luna-Blue", "luna-blue")); themes.add(new Theme(4, "Luna-Amber", "luna-amber")); themes.add(new Theme(5, "Luna-Green", "luna-green")); themes.add(new Theme(6, "Luna-Pink", "luna-pink")); themes.add(new Theme(7, "Omega", "omega")); } public List<Theme> getThemes() { return themes; } }