Dear forum,
I hope I am here in the right topic. Following issue:
- In my table I have 2 additional date fields "start_date" and "end_date".
- Start_date has the default value "now/today".
- The users should enter in the end_date only date values which are greater than the value in the start_date.
- If this is not the case, a java script alert should appear accordingly.
How can I handle this issue on a easiest way?
--> Which hook?
--> Where to code the java script validation?
Thanks in advance
Yilmaz
Date field validation/hook
Re: Date field validation/hook
One of the generated files is named "common.js.php" ... In this file, you can find a function named "tablename_validateData()" (where tablename is the name of the concerned table) ... Usually, the code in this function would look like this:
Returning true means that validation is OK and the form can be submitted ... You can replace the above and add a javascript validation rule similar to this:
Code: Select all
return true;
Code: Select all
if($('FieldName').value==''){
/* display the error message to the user in a non-obtrusive manner using a modalbox */
Modalbox.show(
'<div class="Error" style="width: 90%; margin: 0;">You can\'t leave FieldName empty!</div>',
{
title: "FieldName error!",
afterHide: function(){ $('FieldName').focus(); }
}
);
/* returning false stops form submission */
return false;
};
/* In case of no error, return true to submit the form */
return true;
AppGini plugins to add more power to your apps:
- DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
- Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
- Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.
Re: Date field validation/hook
Dear Ahmad,
many thanks for you hint. It is the right way, but unfortunately I have the following general problem with calling the date field as datepicker:
- I defined 2 variables in common.js.php: "startdate" and "enddate" and assigned them to the values accordingly ($('startsp').value; and $('endsp').value;)
- At first my if condition is due to the equality of those variables
- In case of equal I wanted to see which value is for example assigned to the variable "enddate" (alert (enddate))
- It was only the value year "2013" and not all the other values which are stored in the database as date (see below):
- My idea is to compare the "values" both of the dates (like the php-function strtotime) and alert a message depending of the if condition
- How can I assign the date values (2013-01-16) to the javascript variables?
Thanks in advance
Yilmaz
mysql> select district_id, startsp, endsp from ap_main;
+-------------+------------+------------+
| district_id | startsp | endsp |
+-------------+------------+------------+
| mstahl | 2013-01-16 | 2013-01-30 |
| mreiners | 2013-01-26 | 2013-02-02 |
My code in common.js.php is:
function ap_main_validateData(){
var startdate = $('startsp').value;
var enddate = $('endsp').value;
if(enddate == startdate){alert(enddate); Modalbox.show('<div class="Error" style="width: 90%; margin: 0;"><?php echo addslashes($Translation['datum kleiner']); ?></div>', { title: "<?php echo addslashes($Translation['error:']); ?>Enddatum ", afterHide: function(){ $('customernr_id').focus(); } }); return false; };
many thanks for you hint. It is the right way, but unfortunately I have the following general problem with calling the date field as datepicker:
- I defined 2 variables in common.js.php: "startdate" and "enddate" and assigned them to the values accordingly ($('startsp').value; and $('endsp').value;)
- At first my if condition is due to the equality of those variables
- In case of equal I wanted to see which value is for example assigned to the variable "enddate" (alert (enddate))
- It was only the value year "2013" and not all the other values which are stored in the database as date (see below):
- My idea is to compare the "values" both of the dates (like the php-function strtotime) and alert a message depending of the if condition
- How can I assign the date values (2013-01-16) to the javascript variables?
Thanks in advance
Yilmaz
mysql> select district_id, startsp, endsp from ap_main;
+-------------+------------+------------+
| district_id | startsp | endsp |
+-------------+------------+------------+
| mstahl | 2013-01-16 | 2013-01-30 |
| mreiners | 2013-01-26 | 2013-02-02 |
My code in common.js.php is:
function ap_main_validateData(){
var startdate = $('startsp').value;
var enddate = $('endsp').value;
if(enddate == startdate){alert(enddate); Modalbox.show('<div class="Error" style="width: 90%; margin: 0;"><?php echo addslashes($Translation['datum kleiner']); ?></div>', { title: "<?php echo addslashes($Translation['error:']); ?>Enddatum ", afterHide: function(){ $('customernr_id').focus(); } }); return false; };
Re: Date field validation/hook
Any ideas?
Re: Date field validation/hook
You may need to be a bit patient Yilmaz, it's a new board with only a few experienced members. Ahmad is busy writing a new update and like yourself I'm also waiting for several posts to be answered. I guess it will all come together sooner than later Well I'm hoping it will!
John
John
Re: Date field validation/hook
Yes, of course you are right John. Sorry for my hurry, certainly I will wait.
Yilmaz
Yilmaz
Re: Date field validation/hook - SOLVED
Dear all,
I found in the meanwhile an other way to validate the date fields instead of hooking in the common.js.php:
- Edit tablename_dml.php
- Find the section for tablename_dml.php and replace the condition if empty through the date field comparision as below
- Do the same for update function in the same _dml.php right down.
The only "cosmetic" disadvantage is that you don't have an animated error message field like the output from common.js.php.
Regards,
Yilmaz
I found in the meanwhile an other way to validate the date fields instead of hooking in the common.js.php:
- Edit tablename_dml.php
- Find the section for tablename_dml.php and replace the condition if empty through the date field comparision as below
Code: Select all
function ap_main_insert(){
...
//if($data['endsp']== ''){
if($data['endsp'] < $data['startsp']){
echo StyleSheet() . "\n\n<div class=\"Error\">" . $Translation['error:'] . "<br />" . $Translation['datum kleiner'] . '<br /><br />';
echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
exit;
}
...
//Comparision of a date field with "today"
$heute = date("Y-n-d");
//if($data['date_latest']== ''){
if($data['date_latest'] < $heute ){
echo StyleSheet() . "\n\n<div class=\"Error\">" . $Translation['error:'] . "<br />" . $Translation['Wunschdatum kleiner heute'] . '<br /><br />';
echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
exit;
}
The only "cosmetic" disadvantage is that you don't have an animated error message field like the output from common.js.php.
Regards,
Yilmaz
Re: Date field validation/hook
Thanks much for sharing your solution. This is most useful.
Re: Date field validation/hook
Going back to the common.js.php solution, is there a reason why this code doesn't prevent saving a record with the EndDate earlier than the StartDate (both fields in the ParkingPass table)? I'm kind of new to PHP coding, so please excuse if I've made a stupid mistake.
Code: Select all
function ParkingPass_validateData(){
if($('ResidentID').value==''){ Modalbox.show('<div class="Error" style="width: 90%; margin: 0;"><?php echo addslashes($Translation['field not null']); ?></div>', { title: "<?php echo addslashes($Translation['error:']); ?> Resident ID", afterHide: function(){ $('ResidentID').focus(); } }); return false; };
if($('ReasonForPass').value==''){ Modalbox.show('<div class="Error" style="width: 90%; margin: 0;"><?php echo addslashes($Translation['field not null']); ?></div>', { title: "<?php echo addslashes($Translation['error:']); ?> Reason For Pass", afterHide: function(){ $('ReasonForPass').focus(); } }); return false; };
/* New Date Validation Code */
$TestDate = $('StartDate').value;
if($('EndDate').value < '$TestDate'){
/* display the error message to the user in a non-obtrusive manner using a modalbox */
Modalbox.show(
'<div class="Error" style="width: 90%; margin: 0;">End Date can\'t be earlier than Start Date.</div>',
{
title: "End Date error!",
afterHide: function(){ $('EndDate').focus(); }
}
);
/* returning false stops form submission */
return false;
};
/* In case of no error, return true to submit the form */
return true;
}
-
- Posts: 10
- Joined: 2013-01-30 14:55
- Location: Larissa, Hellas
Re: Date field validation/hook
Another way to validate dates and text is with magic files.
I think in this should help us a little Ahmad.
to validate numbers successfully use this code.
Ahmad, how can I do the same with dates or text?
Α little help is needed, we stuck.
I think in this should help us a little Ahmad.
to validate numbers successfully use this code.
Code: Select all
document.observe('dom:loaded', function() {
$('score').observe('change', function() {
if(isNaN($F('score')) || $F('score') > 100 || $F('score') < 0){
alert('Score must be between 0 and 100!');
$('score').focus();
}
});
});
Α little help is needed, we stuck.
Re: Date field validation/hook
When validating dates in javascript, you need to apply the getTime() function first. Please refer to the sample code at http://stackoverflow.com/questions/6948 ... comparison
AppGini plugins to add more power to your apps:
- DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
- Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
- Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.