0% found this document useful (0 votes)
136 views3 pages

Regex

The document describes predefined regular expressions for common data formats like numbers, emails, etc. It then provides examples of using key filters with regular expressions and masks to restrict input to certain character types in JSF and PrimeFaces form fields.

Uploaded by

john melinda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views3 pages

Regex

The document describes predefined regular expressions for common data formats like numbers, emails, etc. It then provides examples of using key filters with regular expressions and masks to restrict input to certain character types in JSF and PrimeFaces form fields.

Uploaded by

john melinda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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>
------------------------------------------------------------
I need to allow only alphabetic characters [A-Z,a-z] in a PrimeFaces inputText
field.

How can I do this?


Not specific to Primefaces but to underlying JSF:

You can use a regex validator on your input field:

<h:inputText value="#{myBean.myText}" >


<f:validateRegex pattern="[a-zA-Z]+"/>
</h:inputText>
This will work with p:inputText as well.

Adapt the regular expression to your functional requirements.


how to changed regex message? currently it is showing 'Regex pattern does not
match'. @Matt Handy � Irfan Nasim Apr 5 '16 at 8:13
done with validatorMessage="Alpha numaric characters are not allowed in Last Name."
property of inputbox. � Irfan Nasim Apr 5 '16 at 8:21
add a comment

If you need to avoid characters in the view (input text) you can user p:keyFilter
tag as below

<p:inputText id="apePat"
placeholder="Apellido Paterno"
value="#{actualizaDatos.user.apePat}"
autocomplete="off"
label="Apellido Paterno"
validatorMessage="El campo apellido paterno es requerido">

<f:validateRequired/>

<p:keyFilter regEx="/[a-zA-Z�-�\\s\\' ]+/"/>

</p:inputText>
-------------------------------
How do I write a regular expression that includes all keyboard characters except
'~' and ','?
Have you tried this?

[^~,]
Now to exclude characters not in keyboard, I believe you have to include them all.

[a-zA-Z0-9\t\n ./<>?;:"'`!@#$%^&*()\[\]{}_+=|\\-]
Which pretty much covers it (even though it looks like a crazy way to get things
done). Maybe the problem definition can help you add more stuffs to exclude in the
first list [^~,] than try to create a huge list of all keyboard chars
You didn't say what language/tool you're using, but in Java I would go with this
regex:

"[\\p{Print}&&[^~,]]"
That's the intersection of two sets: all printing ASCII characters, and all
characters that aren't a tilde or a comma.
This worked for me:

[A-Za-z0-9-]+[ 0-9A-Za-z#$%=@!{},`~&*()'<>?.:;_|^/+\t\r\n\[\]"-]*

A related one.

I wasted a lot of time searching, so below is the answer I came up with.

I wanted all keyboard keys (including space and tab):

// all keys (including space and tab)


var allKeyboardKeysRegex = /^[a-zA-Z0-9~`!@#\$%\^&\*\(\)_\-\+={\
[\}\]\|\\:;"'<,>\.\?\/ ]*$/;

// example tests
var nonShiftChars = "`1234567890-= qwertyuiop[]\asdfghjkl;'zxcvbnm,./ "
var shiftChars = "~!@#$%^&*()_+{}|:\"<>? ";
var someAlphaNumeric = "aAbB12 89yYzZ";

// test with allKeyboardKeysRegex


allKeyboardKeysRegex.test(nonShiftChars);
allKeyboardKeysRegex.test(shiftChars);
allKeyboardKeysRegex.test(someAlphaNumeric);

Reg-Ex for all supported key board chars worked for me :

/^[a-zA-Z0-9.!@?#"$%&:';()*\+,\/;\-=[\\\]\^_{|}<>~` ]+$/

You might also like