1 2 Previous Next 16 Replies Latest reply on Jul 18, 2017 6:38 AM by Gaz in Oz

    Help for Regular Expression Validation for Apex item

    Er.RJ

      Hi All,

       

      I want to put validation on apex item (Location) using regular expression in a Report. Can anyone help me how to do it. I know how to do it on Page item but apex item am not getting.

      I should restrict user to enter data as 2 digits followed by Hyphen followed by 2 digits  (Refer image below)

      ex: XX-XX           ->   (X is number)

       

       

      Report query Part:  select APEX_ITEM.TEXT(4,SHELF_NO) "Location" from T_ITEMS_VIRTUAL_STOCK

      a2.jpg

       

      Am using Oracle 11g R2, Apex 5

       

      Thanks In Advance

        • 1. Re: Help for Regular Expression Validation for Apex item
          fac586

          Er.RJ wrote:

           

          I want to put validation on apex item (Location) using regular expression in a Report. Can anyone help me how to do it. I know how to do it on Page item but apex item am not getting.

          I should restrict user to enter data as 2 digits followed by Hyphen followed by 2 digits (Refer image below)

          ex: XX-XX -> (X is number)

           

          Report query Part: select APEX_ITEM.TEXT(4,SHELF_NO) "Location" from T_ITEMS_VIRTUAL_STOCK

           

          Am using Oracle 11g R2, Apex 5

          Why are you using a report instead of a tabular form?

           

          Use a tabular form and a standard regular expression validation can be applied to the column.

          • 2. Re: Help for Regular Expression Validation for Apex item
            Er.RJ

            Hi,

             

            As per requirement & my Boss i should use classic report only that's why.

            Can you please help me on that?

             

            Thank you.

            • 3. Re: Help for Regular Expression Validation for Apex item
              fac586

              Er.RJ wrote:

               

              As per requirement & my Boss i should use classic report only that's why.

              Can you please help me on that?

              The best help I can offer is to recommend the use of a tabular form. If there's some compelling reason why this is not possible, then you (or your boss) need to explain why.

              • 4. Re: Help for Regular Expression Validation for Apex item
                Er.RJ

                Hi,

                 

                Actually My boss don't allow us use ready things. So he wont allow to use tabular form. And one more thing is, the project am working is designed by my seniors & left the company so its passed to me. So i cant change it now.

                 

                Thank you.

                • 5. Re: Help for Regular Expression Validation for Apex item
                  Gaz in Oz

                  Using REGEXP_LIKE() to enforce

                  2 digits followed by a hyphen followed by 2 digits

                  there are a few ways using REGEX patterns.

                  Here are two examples:

                  .   REGEXP_LIKE(column, '^([0-9]{2}-[0-9]{2})$')

                  .   REGEXP_LIKE(column, '^(\d{2}-\d{2})$')

                  Read the documentation for further insights into Oracle Regular expressions, linked above.

                   

                  This can be enforced at the table level as a table column check constraint.

                  ALTER TABLE table_name ADD CONSTRAINT
                     enforce_xxhyphenxx CHECK (REGEXP_LIKE(your_column, '^([0-9]{2}-[0-9]{2})$'));

                   

                  In pl/sql as

                  IF NOT REGEXP_LIKE(your_column, '^([0-9]{2}-[0-9]{2})$') THEN

                     ...error

                  END IF;

                   

                  Using sql

                  SELECT your_column,

                         'FALSE'

                  FROM   your_table

                  WHERE  NOT REGEXP_LIKE(your_column, '^([0-9]{2}-[0-9]{2})$')

                  This sql would show all rows that have your_column NOT conforming to xx-xx in your_table.

                  1 person found this helpful
                  • 6. Re: Help for Regular Expression Validation for Apex item
                    Q_STEPHENSON

                    Hi Er.RJ

                     

                    Like fac586 said you would be better off with a tabular table, but as this is not an option try something like the following.

                     

                    What kind of validation do you want? I.E. at point of typing/change on the field or at point of committing your changes.

                     

                    If you want something to fire as it gets changed then you could do something like the following (excuse the roughness of the example):

                    Add a class attribute and id to the the apex item you are generating.  In the id attribute put your recorded unique identifier (this may help you when committing to the database).

                     

                    e.g.

                    APEX_ITEM.TEXT(

                        4,

                        SHELF_NO,

                         NULL,

                         NULL,

                        'class="test" id="something unique"',

                        NULL,

                        NULL) location

                     

                    Then on the JavaScript "Execute when Page Loads" section add the following.

                     

                    $(document).ready(function(){

                        $(".test").on("change", function(event){

                            var text = $(this).val();

                            var id = $(this).attr("id");

                            alert(id+". The field has changed."+text);

                     

                            try {

                                if (text.search(/[^a-zA-Z]+/) === -1){

                                    alert('okay');

                                }else{

                                    $(this).val("");

                                    alert('not okay');

                                   

                                    throw "has numbers";

                             }

                            }

                            catch(err) {

                                alert(err);

                                //message.innerHTML = "Input is " + err;

                            }

                        });

                    });

                     

                    I have put in some basic regex javascript in my example, but you can put your own Validation in javascript, ajax callback to perform something on the DB etc.  You could also append a css class to highlight which field needs correcting.

                     

                    Cheers

                    Q

                    1 person found this helpful
                    • 7. Re: Help for Regular Expression Validation for Apex item
                      fac586

                      Er.RJ wrote:

                       

                      Actually My boss don't allow us use ready things. So he wont allow to use tabular form.

                      Fascinating. They appear to be unfamiliar with the ethos of APEX creators': The Ideal APEX Application (When & Where You Write Code)

                       

                      If that's the case why are reports permitted? They are just as much of a convenience feature as tabular forms. Why aren't you restricted to using PL/SQL Dynamic Content regions? Indeed, why use APEX at all? Shouldn't you be coding everything from scratch in PL/SQL?

                      And one more thing is, the project am working is designed by my seniors & left the company so its passed to me. So i cant change it now.

                      Not even if retaining the existing approach requires much more effort and results in less elegant and robust solution?

                       

                      The OP refers to "Report query Part". What does the rest of the report query look like?

                       

                      Are there any existing validations on the form? If so, how are they implemented? What methods (if any) are used to preserve user input if validation fails?

                      • 8. Re: Help for Regular Expression Validation for Apex item
                        fac586

                        Gaz in Oz wrote:

                         

                        Using REGEXP_LIKE() to enforce

                        2 digits followed by a hyphen followed by 2 digits

                        there are a few ways using REGEX patterns.

                        Here are two examples:

                        . REGEXP_LIKE(column, '^([0-9]{2}-[0-9]{2})$')

                        . REGEXP_LIKE(column, '^(\d{2}-\d{2})$')

                        That's the trivial part. The real problem is how to apply the validation in a way that provides meaningful, accessible information to the user about the nature and location of the error, without losing all of the data entered on the form. This is simple in a standard tabular form region, but very complex in a manually generated form. If the current approach doesn't include support for this then adding it will involve much more effort than simply recreating the region as a tabular form.

                        • 9. Re: Help for Regular Expression Validation for Apex item
                          Er.RJ

                          Hi,

                           

                          Thank you. Am working on it.

                          • 10. Re: Help for Regular Expression Validation for Apex item
                            Er.RJ

                            Hi,

                             

                            Ya am trying all suggestions. I need to fire alert message when i press submit button.

                             

                            Thank you.

                            • 11. Re: Help for Regular Expression Validation for Apex item
                              Er.RJ

                              Hi,

                               

                              Am trying your code with regex as:  ^([0-9]|[A-Z]{2}[-|\][0-9]{2})$       => to get 2 letters or numbers followed by hyphen or slash followed by 2 numbers.

                               

                              Query modified:

                              $(document).ready(function(){

                                  $(".test").on("change", function(event){

                                      var text = $(this).val();

                                      var id = $(this).attr("id");

                                      alert(id+". The field has changed."+text);

                               

                                      try {

                                          if (text.search(/^([0-9]|[A-Z]{2}[-|\][0-9]{2})$/) === -1){

                                              alert('okay');

                                          }else{

                                              $(this).val("");

                                              alert('not okay');

                                            

                                              throw "has numbers";

                                       }

                                      }

                                      catch(err) {

                                          alert(err);

                                          //message.innerHTML = "Input is " + err;

                                      }

                                  });

                              });

                               

                               

                              But when i search its not giving me desire output as i need. Is it my expression wrong or way am doing is wrong. I don't know much about JavaScript. Kindly guide me.

                               

                              Thank you.

                              • 12. Re: Help for Regular Expression Validation for Apex item
                                Gaz in Oz

                                -->to get 2 letters or numbers followed by hyphen or slash followed by 2 numbers

                                To achieve that new requirement, you need something like this

                                '/^(([0-9]{2}|[A-Z]{2})[-|][0-9]{2})$/'

                                From left to right: either 2 digits OR two characters A-Z (uppercase) followed by either a "hyphen "-" or a pipe "|"  followed by 2 digits.

                                 

                                If you want A-Z or a-z then you need to expand [A-Z] to      [A-Z]|[a-z]

                                If mixed case is tolerable then you need to expand [A-Z] to [A-Za-z]

                                 

                                [Edit: after testing, removed \ and replaced with | in above example as the \ is an issue as   has pointed out.]

                                1 person found this helpful
                                • 13. Re: Help for Regular Expression Validation for Apex item
                                  Q_STEPHENSON

                                  You regex should be /^([0-9]|[A-Z]{2}[-|][0-9]{2})$/.  It looks like the "\" is what was preventing a match, assuming your string format is DD-NN (DD being double number or double upper chase characters, and not a mix).  If you're unsure  of regex, there are plenty of regex online testing machines out there that will help you achieve check if your expression is correct.  e.g. https://regex101.com/

                                  1 person found this helpful
                                  • 14. Re: Help for Regular Expression Validation for Apex item
                                    Gaz in Oz

                                    You regex should be /^([0-9]|[A-Z]{2}[-|][0-9]{2})$/.

                                    Not like that, that allows mixed characters and numbers for example, F1-00, 1F-00, FF|0F, etc...

                                    You need to group the 2 characters OR 2 numbers ([0-9]{2}|[A-Z]{2})...

                                    1 2 Previous Next