Wednesday 10 July 2013

Restriction to enter special charecter to user

How to Restrict user from entering special characters using Regular expressions- OAF

In this exercise we have taken hello world page shipped with Toolbox Tutorial.

We are going to restrict user from entering special character on page. For this we have written the code in controller processFormRequest Method & handle the logic on Go Button.

Hence When a user clicks on Go button a validation is done for special characters being entered by user if it is then an error message is shown on the screen as shown in the picture else it will display confirmation message on screen.







Here is the code snippet to handle this validation .

Controller Code

  1. package oracle.apps.fnd.framework.toolbox.tutorial.webui;  
  2. import java.util.regex.*;  
  3.   
  4.   public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)  
  5.   {  
  6.   
  7.     super.processFormRequest(pageContext, webBean);  
  8.   
  9.     if (pageContext.getParameter("Go") != null)  
  10.     {  
  11.       String userContent = pageContext.getParameter("HelloName");  
  12.       String message = "Hello, " + userContent + "!";  
  13.   
  14.      Pattern p = Pattern.compile("[^a-zA-Z0-9\\s]");  
  15.      Matcher m = p.matcher(userContent);  
  16.   
  17.      if (m.find())  
  18.      {  
  19.       throw new OAException("Special Characers not Allowed", OAException.ERROR);  
  20.      }  
  21.   
  22.       else  
  23.       {  
  24.         throw new OAException(message, OAException.INFORMATION);  
  25.     }  
  26.   
  27.     } 

No comments:

Post a Comment