Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Date Validation

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, have any of you done any effecient date validation in their program? Like check if the input is just a space(easy, just use trim()) or if the user input an invalid date...? I need the date in the form

YYYY-mm-dd

I don't want to get 0000-00-00 values, null would be better... Thanks!
 
Jagadeesh Motamarri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apache Commons validator would be helpful.

Here are the details...

Class DateValidator

java.lang.Object
org.apache.commons.validator.DateValidator

public class DateValidator
extends Object

Perform date validations.

This class is a Singleton; you can retrieve the instance via the getInstance() method.

isValid method ----

public boolean isValid(String value,
String datePattern,
boolean strict)

Checks if the field is a valid date. The pattern is used with java.text.SimpleDateFormat. If strict is true, then the length will be checked so '2/12/1999' will not pass validation with the format 'MM/dd/yyyy' because the month isn't two digits. The setLenient method is set to false for all.

Parameters:
value - The value validation is being performed on.
datePattern - The pattern passed to SimpleDateFormat.
strict - Whether or not to have an exact match of the datePattern.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!