简体   繁体   English

Oracle Apex:如何将表单中的数字字段限制为仅允许数字?

[英]Oracle Apex: How can I restrict a numeric field in a form to allow numbers only?

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"?我已经尝试过验证,但没有成功,无论如何我可以通过使用 javascript 代码或“pl/sql 代码”的动态操作来实现这一点吗?

There's absolutely nothing you should do except setting its type to Number field .除了将其类型设置为Number field之外,您绝对不需要做任何事情。

Yes, you can type anything you want into it, but Oracle won't store anything but numbers.是的,您可以在其中输入任何您想要的内容,但 Oracle 不会存储除数字之外的任何内容。 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) .如果必须,请尝试对该项目进行动态操作(我们称之为P13_DEPTNO)

  • When event: Key Press事件发生时:按键

  • Selection type: Item选择类型:项目

  • Item: P13_DEPTNO货号:P13_DEPTNO

  • True action: execute PL/SQL code (with "Items to submit": P13_DEPTNO):真正的动作:执行 PL/SQL 代码(带有“Items to submit”:P13_DEPTNO):

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

The easiest and the fastest way is to set the Field Type as Number field最简单最快的方法是将字段类型设置为数字字段

It will raise the error "Field name must be numeric" and stop the further execution.它将引发错误“字段名称必须为数字”并停止进一步执行。

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所以你可以使用这个https://www.foxinfotech.in/2020/02/oracle-apex-allow-only-integer-value-using-jquery.html

In case anyone needs the answer....万一有人需要答案......

enter this in the page JAVASCRIPT and GLOBAL variable declaration....在页面 JAVASCRIPT 和 GLOBAL 变量声明中输入此内容....

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...然后到你想限制为数字的页面item,右键单击,添加动态动作,when:事件:按键,选择类型:items,items(你选择的item),添加一个true action...并有它执行JS CODE,然后输入此代码...将页面项替换为您的页面项名称...

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

And it will only let you enter numbers into the item.它只会让您在项目中输入数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM