2

how I can make for an inputText accept any letter except allow spaces and special characters?

i tried like this, but is not working...

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
template="/private/commonHomeTemplate.xhtml">
<ui:define name="content">
<h:form id="someForm">
<p:growl id="msg" showDetail="true" life="3000" autoUpdate="true"/>
<p:panelGrid style="100%">
<p:row>
<p:column style="350px">title</p:column>
<p:column>
<h:inputText value="#{someBean.somePropertie}" >
<f:validateRegex pattern="[a-zA-Z]+"/>
</h:inputText>
</p:column>
</p:row>
</p:panelGrid>  
</h:form>
</ui:define>
</ui:composition>

Thank you

Mariah
  • 1,073
  • 1
  • 14
  • 33
  • Sorry, do you want your inputText to accept letters from a-z and A-Z but not spaces and special characters? What are the special characters you don't want to accept? – amedeo avogadro Nov 08 '14 at 10:31
  • thanks i wont accents tildes, etc... #$%&/()=" all that... – Mariah Nov 08 '14 at 16:42

1 Answers1

3

Your regular expression seems to be fine, to see error validation you have to include the <h:message /> tag inside the <h:form /> tag

<h:inputText value="#{someBean.somePropertie}" id="userInputField">
    <f:validateRegex pattern="[a-zA-Z]+"/>
</h:inputText>

<h:message for="userInputField" />

Remember that you cannot use nested <h:form /> inside a JSF page.

amedeo avogadro
  • 595
  • 2
  • 7
  • 14
  • Thanks, i did what u said but its not working anyway :( – Mariah Nov 11 '14 at 15:08
  • Have you checked to no have any nested inside your code? Are you sure to refresh the tag after page submit? How do you submit the data and how do you use the fragment of xhtml you posted? – amedeo avogadro Nov 11 '14 at 15:27
  • i got it.. its working, in fact was a nested form.... sorry i didnt see it !!! its possible do this onkeypress event? – Mariah Nov 11 '14 at 15:51
  • On which tag do you want to catch the onkeypress event? – amedeo avogadro Nov 11 '14 at 16:24
  • on the same input, I do not expect a sumbit to allow the characters, I was checking onkeypress as in this example:http://stackoverflow.com/questions/14596334/how-to-disable-special-characters-and-alphabets-in-primefaces-input-text but i still cant modify to my purpose... – Mariah Nov 11 '14 at 16:37
  • It´s solved, i replace just the function of the example mentioned, something like this: var onlyLetters = /^[a-zA-Z]*$/; function lettersOnly(evt) { var theEvent = evt || window.event; var key = theEvent.keyCode || theEvent.which; key = String.fromCharCode( key ); if( !onlyLetters.test(key) ) { theEvent.returnValue = false; if(theEvent.preventDefault) theEvent.preventDefault(); } } Thanks 4 ur help!!! :D – Mariah Nov 11 '14 at 17:09