Page 1 of 1

Date field validation/hook

Posted: 2013-02-16 16:43
by Yilmaz
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

Re: Date field validation/hook

Posted: 2013-02-16 22:26
by a.gneady
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:

Code: Select all

return true;
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

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;

Re: Date field validation/hook

Posted: 2013-02-18 11:35
by Yilmaz
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; };

Re: Date field validation/hook

Posted: 2013-02-19 10:07
by Yilmaz
Any ideas?

Re: Date field validation/hook

Posted: 2013-02-19 13:58
by Johnk
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

Re: Date field validation/hook

Posted: 2013-02-19 14:10
by Yilmaz
Yes, of course you are right John. Sorry for my hurry, certainly I will wait.

Yilmaz

Re: Date field validation/hook - SOLVED

Posted: 2013-02-19 16:55
by Yilmaz
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

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;
        }
- 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

Re: Date field validation/hook

Posted: 2013-02-19 22:43
by KSan
Thanks much for sharing your solution. This is most useful.

Re: Date field validation/hook

Posted: 2013-02-20 18:41
by MikeB
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;
}

Re: Date field validation/hook

Posted: 2013-02-21 17:22
by pgkounelas
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.

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();
        }
    });
});
Ahmad, how can I do the same with dates or text?
Α little help is needed, we stuck.

Re: Date field validation/hook

Posted: 2013-02-23 07:06
by a.gneady
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