2013-01-24 47 views
5

使用DateBox小部件進行全日期值驗證的最佳方法是什麼?GWT DateBox驗證

像 「1.1」,但以下只是防止不完整輸入允許例如: 「333.333.333

final DateTimeFormat format = DateTimeFormat.getFormat("dd.MM.yyyy"); 
dateBox.setFormat(new DefaultFormat(format)); 

任何建議?

回答

4

事情是這樣的:

try { 
    Date date = DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).parseStrict(value); 
    // Do something with date 
} catch (IllegalArgumentException e) { 
    // Show error message 
} 

您可以使用不同的格式,很明顯,或者你可以嘗試通過一個解析所有格式中的一種,如果你讓你的用戶一個自由輸入日期爲1/1/2013以及Jan 1, 2013,January 1, 2013

+1

Thx! 我擴展了DateBox.DefaultFormat並覆蓋了'parse()'方法來使用'parseStrict()'。 –