« [Tip] Multi-line list design tip | Main | [Tip] Dynamic buttons management »

December 24, 2005

[Tip] Trapping for incorrectly entered dates

by Bob Cusick
Servoy USA

Servoy has a default date setting for each client (you can set it on the Servoy server  http://yourIPaddress:8080/servoy-admin/). What if your users completely disregard the entry "hint" when entering the field - and just blindly type in whatever they think is a valid date? I had to overcome just such a situation and here's a little tip that will hopefully help you as well.

When a user types in an invalid date - assume our default is MM/dd/yyyy - and they type M-d-yy, Servoy cannot parse the data - so Servoy doesn't see the data as being changed. This means that if you try to validate using an onDataChange event on the field - it will fail. Instead, use the code below and attach it to the onFocusLost event of the field instead.

NOTE: This example is hard-coded for a date field called "orderdate"

if(orderdate)
{
    var data = orderdate
 
    var mo = data.getMonth()
    var day = data.getDay()
    var year = data.getYear()
 
    if(year < 0)
    {
         //they typed two digit year
        plugins.dialogs.showErrorDialog( 'Error', 'You must enter a four digit year.',  'OK')
        elements.fld_orderdate.requestFocus(false)
        elements.fld_orderdate.selectAll()
    }
}
else
{
    plugins.dialogs.showErrorDialog( 'Error', 'You must enter the date in the format: ' + i18n.getDefaultDateFormat(),  'OK')
    elements.fld_orderdate.requestFocus(false)
    elements.fld_orderdate.selectAll()
}

| Posted by David Workman on December 24, 2005 at 12:29 PM in Tips | Permalink

Comments

Post a comment