最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Oracle Apex: How can I restrict a numeric field in a form to allow numbers only? - Stack Overflow

programmeradmin5浏览0评论

I have an item in a form which is a number field and I want to somehow make sure that only numbers are accepted in the field. If users enter letters I want the field to reject it.

I already tried a validation but it was unsuccessful, is there anyway I get make this happen via a dynamic action with either a javascript code or a "pl/sql code"?

I have an item in a form which is a number field and I want to somehow make sure that only numbers are accepted in the field. If users enter letters I want the field to reject it.

I already tried a validation but it was unsuccessful, is there anyway I get make this happen via a dynamic action with either a javascript code or a "pl/sql code"?

Share Improve this question edited Sep 28, 2020 at 19:37 Littlefoot 143k15 gold badges40 silver badges63 bronze badges asked Sep 28, 2020 at 19:00 ChepeChepe 771 silver badge11 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

There's absolutely nothing you should do except setting its type to Number field.

Yes, you can type anything you want into it, but Oracle won't store anything but numbers. It'll raise the "Field name must be numeric" error and stop.

Why would you want to reinvent the wheel?


If you must, try with dynamic action on that item (let's call it P13_DEPTNO).

  • When event: Key Press

  • Selection type: Item

  • Item: P13_DEPTNO

  • True action: execute PL/SQL code (with "Items to submit": P13_DEPTNO):

    begin
      if not regexp_like(:P13_DEPTNO, '^\d+$')
      then
        raise_application_error(-20000, 'Digits only');
      end if;
    end;
    

In case anyone needs the answer....

enter this in the page JAVASCRIPT and GLOBAL variable declaration....

function setInputFilter(textbox, inputFilter) {
  ["input", "keydown", "keyup", "mousedown", "mouseup", "select", "contextmenu", "drop"].forEach(function(event) {
    textbox.addEventListener(event, function() {
      if (inputFilter(this.value)) {
        this.oldValue = this.value;
        this.oldSelectionStart = this.selectionStart;
        this.oldSelectionEnd = this.selectionEnd;
      } else if (this.hasOwnProperty("oldValue")) {
        this.value = this.oldValue;
        this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
      } else {
        this.value = "";
      }
    });
  });
}

Then go to the page item you want to restrict to numbers, and right click, add dynamic action, when: event: key press, selection type: items, items (item you have selected), add a true action...and have it execute JS CODE, then enter this code... replace the page item with your page item name...

setInputFilter(document.getElementById("PXX_XXXX"), function(value) {
  return /^-?\d*$/.test(value); });

And it will only let you enter numbers into the item.

you can solve this by using a bination of oninput and pattern matching to ensure only numeric input:

<input type="text" id="numericInput" name="numericInput" oninput="this.value=this.value.replace(/[^0-9]/g,'');">

So, put oninput="this.value=this.value.replace(/[^0-9]/g,'');" into Advance --> Custom Attributes of the item.

Done!!

Sometimes you do need to prevent users from inserting anything else but numbers. Especially if this field is used for calculations. So you can use this https://www.foxinfotech.in/2020/02/oracle-apex-allow-only-integer-value-using-jquery.html

I have created a blog post for this usecase. Please refer the below link

https://akilramesh.blogspot./2021/10/allow-only-numbersdecimal-in-numbertext.html

发布评论

评论列表(0)

  1. 暂无评论