Regular Expression Validation in JSF

When most people look at the list of components in ADF Faces they mostly focus on the visual components - and that's understandable, but if you look closer you'll find that there are also non-visual components included such as several validators.

Here is an example of one useful validator - the regular expression validator.

Let's suppose that you want your JSF field to allow only Numeric values - all you need to do is add to it the ADF Faces provided regular expression validator and then provide the right regex format.
For Example:


<af:inputText label="Label 1" autoSubmit="true">

 <af:validateRegExp pattern="[0-9]{1,10}"

                               noMatchMessageDetail="Only Numbers Please"/>

</af:inputText>

<af:commandButton text="commandButton 1"/>


Now run your page and try entering characters to the field and leaving it.
You'll immediately get an error message with the text you provided (implemented using Javascript).

 

Just one more thing that ADF Faces give you to make life easier...

Comments:

Very useful example. Thanks! Btw. I had to use "messageDetailNoMatch" for the error message

Posted by guest on October 25, 2011 at 02:14 AM PDT #

Hi Shay,

I'd like to know if it is safe or advisable that I use regex to validate email address in Oracle ADF in a real-world application.

Thanks a lot,
Mylene

Posted by guest on October 31, 2011 at 05:54 PM PDT #

Mylene - using regex would be good - I would also suggest that you do the check on the model layer with an attribute level validator - this will eliminate getting wrong data if someone uses your business service in another UI.

Posted by guest on October 31, 2011 at 08:46 PM PDT #

Hi Shay,

Thanks a lot for your reply. Yes, I'll do what you have suggested -> do a check on the model layer with an attribute level validator. I'm actually new to Oracle ADF.. I guess this task given to me would be a good starting point. Your blog has been very useful! :)

Regards,
Mylene

Posted by guest on October 31, 2011 at 08:49 PM PDT #

i have this zip code 1034 xj. so i want to make validation rule with regular expression(Jdeveloper) what would be the code.

Thanks for help

Posted by Blend on January 15, 2012 at 06:33 AM PST #

Hi Shay,
This sample is Great.

Thanks a lot. Your blog always saves me.

Regards,
Udaya

Posted by Udaya on February 13, 2012 at 05:14 PM PST #

Hi Shay,

Do you know if it is possible to add this regular expression validation if I am constructing the inputText component programmatically? And if yes, how?

Thanks!

Posted by guest on May 10, 2012 at 08:09 AM PDT #

guest - I think you can call a setValidator method on your inputText component in your backing bean.

Posted by Shay on May 10, 2012 at 11:57 AM PDT #

Thanks Shay,

I managed to do this by creating a org.apache.myfaces.trinidad.validator.RegExpValidator validator and using inputText.addValidator(validator);

It works as expected, the only problem is that validation is done when the page is submitted, not when the focus on the inputText is lost, like it happens when using <af:validateRegExp>. Do you know a simple way of doing this? (I was thinking that one option would be using onValueChange and adding custom validation messages)

Thanks,
Georgiana

Posted by Georgiana on May 11, 2012 at 05:08 AM PDT #

Sorry, my mistake, I forgot to setAutoSubmit to true on inputText. Thanks!

Posted by Georgiana on May 11, 2012 at 05:25 AM PDT #

Is there a way to re-run the regex after each key stroke, and not when the focus is lost?

Posted by guest on August 31, 2012 at 08:48 AM PDT #

guest - to execute logic after each key stroke you'll need to use JavaScript integration look into the keyDown clientListener. For more on JavaScript integration see:
http://download.oracle.com/otn_hosted_doc/jdeveloper/11gdemos/adf-insider-javascript/adf-insider-javascript.html

Posted by Shay on August 31, 2012 at 09:35 AM PDT #

Hi Shay,
I am trying to validate the email address using the regexp [A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4} in jdev 11g. When I run the page and enter the email address, it keeps showing wrong format error message though correct format. I tried few other regexp, still I have the same problem. Could you please help me solve this problem?

Thanks,
Sush

Posted by guest on January 03, 2013 at 02:15 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

me
I'm a Director of Product Management for the Oracle Java Development Tools.
Follow me:
Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today