TheNewby
TheNewby

Reputation: 107

Primefaces InputMask optional digits

I'm working on a small Project using Primefaces.

I'm trying to display value from my database with special formation I store a String in my database which Looks like this 40;99;1;0;0;12 These are six different numbers seperated with a ;and they have a range between 0-99.

I want to display this value in an InputMask to make sure that the user doesn't forget the ;. Now I have a problem with the format.

I'm using this

<p:inputMask id="myID" placeholder="Value" value="#{myClass.value}" mask="9?9;99;99;99;99;99"/>

I'm trying to say that the mask contains six "fields" for a number input and each field can contain a number between 1-99.

The problem is if the user writes the input as this 30;10;0_;0_;0_;4_Primefaces converts it to 30;10;00;04;__;_

Is there a good way to let the mask know that it can be a number with one or two digits? Or is there a way to say, if input has just one digit put a 0 first

Upvotes: 3

Views: 3562

Answers (1)

Apostolos
Apostolos

Reputation: 10463

I dont know if this solution fits your needs, but you can define a custom mask

$.mask.definitions['h'] = "[0-9]{0,1}";
$("#form\\:your_input_mask_id").mask("hh;hh;hh;hh;hh;hh");

in your document.ready function. This will leave the _ character as is, but I guess you can handle it in your code when splitting the string and handling each part. I've put form\: because you generally put these inputs inside a form with id form. Change accordingly!

This has the downside though, that if you dont enter something, it will also validate, regardless of not being between 1 and 99.

Upvotes: 1

Related Questions