如何在datepickerfield上验证生日?

时间:2012-06-05 15:11:14

标签: sencha-touch extjs sencha-touch-2 sencha-architect

我需要在datepickerfield上验证生日,但我不知道尝试。

我有这段代码:

datepickerfield

{
    xtype: 'datepickerfield',
    id: 'dob',
    name: 'dates',
    useClearIcon: true,
    renderData: {
        placeHolder : 'dd/mm/YYYY'
    },
    label: '*Born of birthday',
    picker: {
        doneButton: 'Select',
        cancelButton: 'Cancel',
        slotOrder: ['day', 'month', 'year'],
        yearFrom: 1925,
        yearTo: 2012,
        hideOnMaskTap: true,
    }
}

并验证我

if(Ext.getCmp('dob').getValue().format('d/m/Y') < new Date().format('d/m/Y')) {
    Ext.Msg.alert('Adult')
} else {
    Ext.Msg.alert('Minor')
}

请帮帮我。

1 个答案:

答案 0 :(得分:1)

将此侦听器添加到datepickerfield对象:

listeners:{
  change:function(picker, birthDate){
    var today = new Date(),
        age = today.getFullYear() - birthDate.getFullYear(),
        m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    if(age >= 18){
      console.log('Adult');
    }
    else{
      console.log('Minor');
    }
  }
}

希望这有帮助