keyFilter for Phonenumber not working

Community Driven Extensions Project
Post Reply
santhoshsamala
Posts: 26
Joined: 23 Feb 2012, 18:10

10 Apr 2012, 22:42

I'm trying to validate a US phone number with primefaces extensions keyFilter. The code is

Code: Select all

<p:inputText id="amBusinessContactNum" 
								value="#{customerOverviewAction.customerOverviewBean.amBusinessContactNum}"
								label="amBusinessContactNum" >
								<pe:keyFilter regEx="/^([\(]{1}[0-9]{3}[\)]{1}[\.| |\-]{0,1}|^[0-9]{3}[\.|\-| ]?)?[0-9]{3}(\.|\-| )?[0-9]{4}$/"/> 
								</p:inputText>
Its not considering any input for this, infact it is not allowing me to enter anything in that text box . do i need to change anything here?
Using primefaces3.2, jboss7.0.2, weld 1.1.5

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

11 Apr 2012, 12:17

Hi.

I have debugged and understood your problem. Reg. Exp. tested every input character. It works as

Code: Select all

var ok = re.test(c);

if(!ok) {
    e.preventDefault();
}
Where "re" is your reg. expression /..../ and "c" current sign you input. Sure, it will not work. For instance this reg exp. /[0-9][0-9]/ returns false if you input 4, but true if you input 44. Because you should test the entire input string against reg. expression. Use testFunction instead. In the test function you can read the input string with jQuery and test with reg. expression every input position. For instance, you know that you 1. character should be (. You can read input string from p:inputText, call getCharAt(0) and compare with (. I can't see any other way. You can test if you input e.g. only digits or only alphanumeric values, but exactly test char for char is difficult.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

Post Reply

Return to “Extensions”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests