Can an error be thrown -> Characters in a Numeric field

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
User avatar
gregsintx
Posts: 15
Joined: 2015-05-09 06:25

Can an error be thrown -> Characters in a Numeric field

Post by gregsintx » 2015-05-29 03:32

If someone enters characters into a numeric field, and the click Save - it just saves and throws away what they entered with no error message.

Can this be caught and an error message be given that they should enter numbers only?

User avatar
a.gneady
Site Admin
Posts: 1354
Joined: 2012-09-27 14:46
Contact:

Re: Can an error be thrown -> Characters in a Numeric field

Post by a.gneady » 2015-06-04 10:50

Open the generated common.js.php file in a text editor and find a function named tablename_validateData (where tablename is the name of the concerned table) ... before the "return true" line, add this code:

Code: Select all

if(!$j('fieldname').val().match(/^-?\d*\.?\d*$/)){
     modal_window({
         message: '<div class="alert alert-danger">Only numbers are allowed in this field</div>',
         title: "Error in fieldname!",
         close: function(){
             $j('#fieldname').focus();
         }
    });
    return false;
};
Replace 'fieldname' above with the concerned field name.
:idea: AppGini plugins to add more power to your apps:

User avatar
gregsintx
Posts: 15
Joined: 2015-05-09 06:25

Re: Can an error be thrown -> Characters in a Numeric field

Post by gregsintx » 2015-06-09 06:10

Thanks - this worked great!

Type validation would be a good thing for any numeric fields I think.

User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Re: Can an error be thrown -> Characters in a Numeric field

Post by shasta59 » 2015-06-14 15:04

I have done this but I prefer to do it by placing similar code on the actual tablename_templateDV.html file.

I have code, which as soon as the field is changed/modified, checks the contents of the field and advises if incorrect characters are used. This way it is done right in front of the user at the time of the incorrect characters being used. Ahmad's method uses less processor overhead but the hit is so very minimal it is not significant in my case.

I use:

Code: Select all

<script>
$j(document).ready(function(){
$j('#fieldname').change(function(){
	.....
The above is the first couple of lines and the code follows. Biggest advantage to this method is the user gets an alert right away rather than when the Save Record button is pressed.

The first line is the code for when the document is ready
The next line has it look at the field with the data as soon as that field is the focus and data is entered/changed.

Alan
Calgary, Alberta, Canada - Using Appgini 5.50 -

Post Reply