X

Shay Shmeltzer's Oracle Development Tools Tips

  • December 3, 2007

Regular Expression Validation in JSF

Shay Shmeltzer
Director of Product Management - Oracle

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...

Join the discussion

Comments ( 15 )
  • guest Tuesday, October 25, 2011

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


  • guest Tuesday, November 1, 2011

    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


  • guest Tuesday, November 1, 2011

    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.


  • guest Tuesday, November 1, 2011

    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


  • Blend Sunday, January 15, 2012

    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


  • Udaya Tuesday, February 14, 2012

    Hi Shay,

    This sample is Great.

    Thanks a lot. Your blog always saves me.

    Regards,

    Udaya


  • guest Thursday, May 10, 2012

    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!


  • Shay Thursday, May 10, 2012

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


  • Georgiana Friday, May 11, 2012

    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


  • Georgiana Friday, May 11, 2012

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


  • guest Friday, August 31, 2012

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


  • Shay Friday, August 31, 2012

    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


  • guest Thursday, January 3, 2013

    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


  • guest Tuesday, February 9, 2016

    Hi Shay,

    I'd like to write a validation to validate the Name of a user in the user form should be starts with Capital. For example the user should enter only "Employee". Please suggest what kind of validation I can use to achieve this.

    Thanks,

    Kesav


  • andrejs Wednesday, February 17, 2016

    ^[А-Z]{1}.*$

    Matches any word starting with a single capital character. Hope this helps.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.