Page 1 of 1
Can an error be thrown -> Characters in a Numeric field
Posted: 2015-05-29 03:32
by gregsintx
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?
Re: Can an error be thrown -> Characters in a Numeric field
Posted: 2015-06-04 10:50
by a.gneady
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.
Re: Can an error be thrown -> Characters in a Numeric field
Posted: 2015-06-09 06:10
by gregsintx
Thanks - this worked great!
Type validation would be a good thing for any numeric fields I think.
Re: Can an error be thrown -> Characters in a Numeric field
Posted: 2015-06-14 15:04
by shasta59
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