I have an APEX app that I am working on and need to make a field to only accept numeric values.
I want to have a validation that displays "Only numeric values allowed" whenever the user inputs letters or other characters that are not numeric.
How can I achieve this?
Thanks in advance
If you use the translate function, you can "detect" whether string contains something but digits (this example allows decimal dot as well):
SQL> with test (col) as
2 (select '1234' from dual union all -- valid
3 select '12a4' from dual union all -- invalid
4 select '12.4' from dual union all -- valid
5 select 'abcd' from dual union all -- invalid
6 select '1#34' from dual -- invalid
7 )
8 select col, translate(col, 'x0123456789.', 'x') result
9 from test;
COL RESU
---- ----
1234
12a4 a
12.4
abcd abcd
1#34 #
SQL>
So, if the result is NULL, then it is a valid value.
In Apex, you'd create validation (function that returns error text) which looks like this:
if translate(:P1_ITEM, 'x0123456789.', 'x') is not null then
return 'Only numeric values allowed';
else
return null;
end;
These field definitions are more about the inherent server-side validations that will run on your behalf to ensure the data submitted is numeric.
I think the best you're going to get is having some client side validation as well that gives the user immediate feedback - either has message, or auto-strip. I'm not sure if you can force a HTML element to only allow numerics.
If you really need to validate after each new character, you can try to add Dynamic Action based on Key Release event and Execute JavaScript code when true. Something like this
if ( $v("P2_NEW") !== ""
&& isNaN($v("P2_NEW"))) {
alert("Not a number");
}
Ok after some hours of trying this and that, the following is what has worked for me.
Click in the page. In the "Function and Global Variable Declaration" section add this code:
function isInputNumber(evt){
var ch = String.fromCharCode(evt.which);
if(!(/[0-9]/.test(ch))){
evt.preventDefault();
}
}
In the "Page HTML Body Attribute" section add this code:
onkeypress="isInputNumber(event)"
and thats it. This will prevent the user from entering letters into the input field.
Related
I want to call validation when
query return at least one row like ( select 1 from table where id = 1 )
and
when request "add_data" is called
Don't know what to select here:
The value of request can be accessed via the bind variable REQUEST . To tackle your specific problem you could add that to your condition of type 'Rows returned' like this:
SELECT 1
FROM table
WHERE (
id = 1 AND
:REQUEST = 'add_data'
)
If you're more a fan of pl/sql you could take the condition type 'PL/SQL Function Body' with code like this:
DECLARE
l_dummy NUMBER;
BEGIN
-- quit here if request value doesn't match
IF :REQUEST != 'add_data' THEN
RETURN false;
END IF;
-- check if we have rows in table.
BEGIN
SELECT 1
INTO l_dummy
WHERE EXISTS (select 1 from table where id = 1)
EXCEPTION WHEN NO_DATA_FOUND THEN
RETURN false;
END;
RETURN true;
END;
This is more code but you could find it easier to read which can be an advantage for maintenance later on.
The way I understood it, add_data request is related to a button you push. If that's so, then - if you don't do anything, request name equals button name - so let's pretend that button name is ADD_DATA. Therefore:
in "When Button Pressed" property select button's name (ADD_DATA, right?)
in "Condition Type" pick the condition you mentioned ("SQL query returns at least one row")
I am currently using apex 19.1. I have this problem where I can't (or don't know how to) select certain columns from checkbox meaning I have this checkbox
which gives me the ability to check the columns names I want to use that output (:P3_COLUMN) from the check box to select a specific columns in a table. My solution was :
select :P3_COLUMN
from INPUT_TABLE$
I also tried :
select case :P3_COLUMN when 'currency' then currency when 'nationality' then nationality end as test from input_table
which gave me this output
and
DECLARE
str varchar2(100);
BEGIN
str := 'select ' || replace(:P3_COLUMN, ':', ',') || ' from input_table';
execute immediate str;
END;
which gave me this error
I don't know what to do, any help will be really appreciated.
Here's a walkthrough (my page is #51). Suppose that we want to display some column from Scott's DEPT table.
create a region whose type is classic report
create a page item (let's call it P51_COLS which is a select list item; its source is a query which looks like this:
select column_name d,
column_name r
from user_Tab_columns
where table_name = 'DEPT'
Page action on selection should be "Submit page"
region's source should be a PL/SQL function body that returns a SQL query and look like this:
return 'select case when :P51_COLS = ''DEPTNO'' then to_char(deptno )
when :P51_COLS = ''DNAME'' then dname
when :P51_COLS = ''LOC'' then loc
end as result
from dept';
Its "Page items to submit" should be set to P51_COLS
That's it ... run the page; select any column from the select list item and the result should be displayed.
Yes, I know - the query itself looks stupid as you have to name all cases. For some reason, Apex expects literally return 'select ...' statement. Concatenation, replace function, ... won't work. Perhaps someone knows why or - even better - can demonstrate how to workaround it. Meanwhile, try what's been written above.
first option use server side condition on the columns.
second option use dynamic sql> create function returns sql statement> call the function in your region source.
I have an item to enter the email id "item_email"
I need to give validation for email id item "item_email" that it should not accept more than one email ids.
Is there is any possible to do this, please help me to proceed on this.
A simple validation; count number of #s. For example:
SQL> with test as
2 (select 'lf#gmail.com' email from dual union
3 select 'lf#gmail.com bf#gmail.com' from dual
4 )
5 select regexp_count(email, '#') cnt
6 from test;
CNT
----------
1
2
SQL>
If such a query returns 1, item contains one "e-mail" address.
However, consider yet another validation - whether value entered into that item really looks like an e-mail. Because, if someone enters only "#", that would pass the previous validation, but it certainly isn't a valid e-mail address.
On the other hand, maybe you already perform that validation. In that case, disregard that objection.
[EDITED by LF, after reading OP's comment]:
Create a validation on the e-mail item (whose name is, for example, :P3_E_MAIL). Its type should be "PL/SQL function (returning error text)", and its code
if regexp_count(:P3_E_MAIL, '#') > 1 or
not regexp_like(:P3_E_MAIL, '^[a-zA-Z0-9._%-]+#[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}$')
then
return ('E-mail address is incorrect');
end if;
See whether you're satisfied with such a code.
I need to know how to make Oracle database to error out. The reason is to try to catch an exception in the JDBC.
Calls to the database are invoked via web services that ultimately use JDBC calls to the database. So, consumers interface to the whole thing through SOAP requests. The majority of requests take xsd:string or/and xsd:date.
One way, for instance, I'm doing this is to set a character larger than 4000 character in the WHERE clause for a VARCHAR2 field of (BYTE 4000). Are there other ways of forcing the database to throw an error?
If you can't add objects to the database, then you'll need to use an error that can be created in pure SQL. Here are a few options:
Invalid date (ORA-01839): select to_date('2/30/2014','mm/dd/yyyy') from dual
Invalid number (ORA-01722): select cast('aaa' as number) from dual
Invalid math (ORA-01476): select 1/0 from dual
Invalid identifier (ORA-00904): select this_doesnt_exist('aaa') from dual
Invalid format (ORA-01810): select to_date('10','mmmmmmmmm') from dual
You could possibly do one of the following:
Use a procedure like A.B.Cade proposed;
Divide 1/0 or any other low-level error.
My option would be 1, with a few modifications:
create function raise_custom_error
( error_no number
, message varchar2
)
return number
as
begin
raise_application_error(error_no, message);
return 0;
end;
/
Use it like this:
select case
when 1=1 /*bad condition*/
then raise_custom_error(-20163, 'My custom error')
else 0
end
from dual
I have a report region on my page and it's getting data from a students table. Within that table, there's a column called cv_lodged which is either 1 or 0 representing Yes or No. In the report it shows this columns data as 0 or 1, I want to change it to show Yes or No.
How can I do this?
You can put a CASE statement in your query
SELECT (CASE WHEN cv_lodged = 1
THEN 'Yes'
ELSE 'No'
END) cv_lodged,
other_columns
FROM students
If this is something that is likely to be common, you're probably better served creating a function that converts your pseudo-boolean data to a string. Something like
CREATE FUNCTION my_bool_to_str( p_num IN NUMBER )
RETURN VARCHAR2
IS
BEGIN
IF( p_num = 1 )
THEN
RETURN 'Yes';
ELSE
RETURN 'No';
END IF;
END;
that you would call in your query
SELECT my_bool_to_str( cv_lodged ) cv_lodged,
other_columns
FROM students
I've usually just created a static LOV in Shared Components called YES1_NO0, with 2 entries: 'Yes' -> 1, 'No' -> 0.
In Interactive Reports you can then change these columns. To do this, edit the column of the IR. Change 'Display Type' to 'Display as Text (based on LOV, escape special characters)'. Then under 'List of Values' set 'Column Filter Type' to 'Use Named List of Values to Filter Exact Match' and select the named LOV.
But sure, you can totally do this in SQL.
Currently, there is a simpler way to do this.
First open the APEX page with the "Yes/No" item as developer and click on the item to open the page item settings.
Change the Settings value from Use component settings to Custom.
Afterwards change the Yes Value to 1 and the No Value to 0.
This solution has some advantages:
No need to decode the value 1 or 0 to Y or N (select decode(mycolumn, 1, 'Y', 'N') from mytable where id = 123;) or select case when ...
No need to decode the submitted value back to 1 or 0 from Y or N