11 Replies Latest reply on Aug 23, 2013 8:50 PM by Fred-L

    How to check apex item is numeric

    SAYYED

      Hi,

       

      I  am having apex item called P23_USER_GROUP, i would like to check this item value provided by user is numeric or non-numeric.

       

      how can i do this , please suggest me

       

      Thanks and regards,

      Ibrahim Sayyed.

        • 1. Re: How to check apex item is numeric
          Nick Buytaert

          Hey Ibrahim,

           

          Create an item of type "Number Field". APEX will automatically validate whether the value entered by the end user is a valid number.

           

          Kind regards,

          Nick

          • 2. Re: How to check apex item is numeric
            SAYYED

            I have created Master details form ,this item(P23_USER_GROUP)  is automatically created by apex so for this there is  no such option like item of type "Number Field".

            • 3. Re: How to check apex item is numeric
              Nick Buytaert

              Okay then. It is possible to create a validation that checks for a numeric value. Follow these steps:

               

              • Create a new validation
              • Validation level is page item
              • Select the P23_USER_GROUP page item
              • Assign the validation a sequence and name
              • Validation type is string comparison
              • Then select Item/Column is numeric
              • ...

               

              That should do it.

              • 4. Re: How to check apex item is numeric
                SAYYED

                Hi,

                 

                I have created as you said one validation,

                 

                and one more validation like validation type Function Returning Boolean

                 

                IF :P23_USER_GROUP BETWEEN 0 and 100 THEN

                  RETURN TRUE;

                ELSE

                RETURN FALSE;

                END IF;

                 

                in this case if i enter characters and click on submit button then it is validating from back-end

                "ORA-06502: PL/SQL: numeric or value error: character to number conversion error"


                if disable second validation i.e  Function Returning Boolean then

                other validation i.e numeric validation is working fine.


                 

                 

                Thanks and regards,

                Ibrahim Sayyed.

                • 5. Re: How to check apex item is numeric
                  Nick Buytaert

                  I really advise you to use the "Number Field" item type. It has a minimum and maximum setting which automatically gets validated on page submit. Just create a new page item of type "Number Field", copy the settings, and remove the old P23_USER_GROUP item. Should take no more than two minutes I believe. This way you let APEX take care of these standard validations.

                  1 person found this helpful
                  • 6. Re: How to check apex item is numeric
                    SAYYED

                    Hi Nick,

                     

                    Thanks for your help.

                     

                    I have done lot of changes on it, and DML and some other operations tacking care automatically by application, so i can't go head for a separate page item.

                     

                    Please suggest me if any other way is there other than this.

                     

                    Is there any numeric validation option available with oracle db 11g, that also fine for me.

                     

                    Thanks and regards,

                    Ibrahim Sayyed.

                    • 7. Re: How to check apex item is numeric
                      Tom Petrus

                      I don't get the problem. You can change the item type of an item without any trouble... You simply edit your item and change the type. As soon as you select number you will get the number settings. Sure, apex may have created this item through a wizard but that doesn't stop you from changing the item to suit your needs. You don't need to remove it and recreate it or anything. And you know what? It probably won't matter as most things referencing this item would not change at all, only things directly hooked up to the item such as computations/validations/dynamic actions.

                      If you really REALLY don't want to do that... Well, that'd be odd. Not willing to change an item but willing to deal with manual validation? Let's see why your experiment with 2 validations fails: every validation fires. A failed validation will NOT stop following validations from executing and you can easily see this from the debug log. The numeric validation will fail and then the second validation will execute. Typed in "x"? Tough luck. Of course, you can go and work around that by doing a "to_number" and use an "exception when xxx" to account for it failing. But seriously - this is in the number item type.

                      1 person found this helpful
                      • 8. Re: How to check apex item is numeric
                        Nick Buytaert

                        @Sayyed: As Tom says, changing the type of a page item barely influences other components, especially when you change from text to number. Even in case you don't like the number item type, it's always possible to change it back to text again. Trust me, APEX can cope with that.

                         

                        @Tom: Yeah, of course you can change an item's type without having to delete and recreate it... Thanks for correcting me.

                        • 9. Re: How to check apex item is numeric
                          SAYYED

                          Hi ,

                           

                          Thanks for all,

                           

                          I go to some alternate method by placing onkeyup="this.value=this.value.replace(/[^0-9]/g,''); in item  .

                           

                          this will allow only numbers.

                           

                          Thanks and regards,

                          Ibrahim Sayyed.

                          • 10. Re: How to check apex item is numeric
                            Tom Petrus

                            I would like to point out that no, it does not. It is javascript, and thus by its nature it is client side. It is great, but if you want to be sure of the data entered then you should still provide a server-side validation too. All it takes for me to cancel out your code is to delete the onkeyup attribute through any developer tool in my browser. Take that in consideration.

                            And it isn't obvious if you would allow numbers with decimals between 0 and 100, but if yes, then the regexp will fail. Allowing for a comma in the regexp will then once again lead to the possibility of erroneous input. (eg double comma)

                            Also, why would you use the onkeyup attribute at all? Use a dynamic action, and at least do it for yourself. Placing this in a dynamic action would highly increase the visibility of this piece of code on your page. And personally I hate attribute event handlers

                            • 11. Re: How to check apex item is numeric
                              Fred-L

                              Hi,

                               

                              Using an Number Field item is probably the best solution but if you want keep a Text Field item it's possible to check if the value in numeric and also if between 0 and 100 in only 1 validation.

                               

                              Create a validation "Function Returning Error Text"

                              declare
                                v_number   number;
                              begin
                                v_number := to_number(:P23_USER_GROUP);
                                if not (v_number between 0 and 100) then
                                  return('Not between 0 and 100');
                                end if;
                              
                                exception
                                  when INVALID_NUMBER then
                                    return('Not numeric');
                                  when VALUE_ERROR then
                                    return('Not numeric');
                                  when OTHERS then
                                    return('Not numeric');
                              end;
                              
                              

                               

                              if you want you can remove lines

                                  when INVALID_NUMBER then
                                    return('Not numeric');
                                  when VALUE_ERROR then
                                    return('Not numeric');
                              
                              

                              and only keep

                                  when OTHERS then
                                    return('Not numeric');
                              
                              

                              depends if you need to have different error messages.

                               

                              Details about exception are here: Handling PL/SQL Errors

                               

                              Fred