user19726169
user19726169

Reputation: 1

Data driven restriction of characters in an Apex Field

I have an input field in my Apex application (v20.1.0) which currently prohibits a pre-defined list of restricted characters using the column Attributes to run a javascript function

    onkeypress="return alpha(event)"
    
    function alpha(e) {
      var k = e.keycode;
      if (k==59)
        {return false;}
      else
        {return true;}
    }

This is fine for a simple case such as restricting one character, but if we have a user-defined list of prohibited characters what solution would be best. I could have a system parameter that could contain a list of prohibited characters in a single string, or a table with a record for each restricted character so that these could be maintained by a user/script. The question is what is the most flexible solution for implementing these restrictions dynamically so that we do not need a code change on each occasion the business decides to update their prohibited special character set

First time I've posted here so hopefully it makes sense and someone out there can come up with a good solution

Upvotes: 0

Views: 204

Answers (1)

Littlefoot
Littlefoot

Reputation: 142705

I'd store prohibited characters into a table and create validation (function that returns error text) as

declare
  l_cnt number;
begin
  select count(*)
  into l_cnt
  from prohibited_characters p
  where instr(:P1_TEXT_ITEM, p.character) > 0;

  if l_cnt > 0 then
     return 'Text contains character that is not allowed';
  end if;
end;

That's rather simple; you can then improve it and list these characters (because, how is user supposed to know which character they entered is considered to be "prohibited"), but - that's a general idea.


Why is that approach flexible? Because you can easily add (or remove) rows from the table and code you wrote doesn't have to be modified.

If you're validating text in various items and/or pages, it would be a good idea to create a stored function which performs validation and returns error text or NULL (which means that everything is OK). Doing so, Apex validation would just call the function, and you'd maintain only it - the function - if necessary, but that's just one program module - you wouldn't have to visit all pages/validations to fix what's being changed.

Upvotes: 1

Related Questions