I have a date input box in my lift application, and I want to check that a user-entered date is in correct format: dd/mm/yyyy.
How can I write a reg
I wouldn't use a regex, but SimpleDateFormat (which isn't that simple, as we will see).
A regular expression which handles to allow 28 and 30 as day, but not 38, different month-lengths and leap years, might be an interesting challenge, but not for real world code.
val df = new java.text.SimpleDateFormat ("dd/MM/yyyy")
(I assume M as in big Month, not m as in small minute).
Now, let's start with an error:
scala> df.parse ("09/13/2001")
res255: java.util.Date = Wed Jan 09 00:00:00 CET 2002
hoppla - it is very tolerant, and wraps months around to the next year. But we can get it with a second formatting process:
scala> val sInput = "13/09/2001"
sInput: java.lang.String = 13/09/2001
scala> sInput.equals (df.format (df.parse (sInput)))
res259: Boolean = true
scala> val sInput = "09/13/2001"
sInput: java.lang.String = 09/13/2001
scala> sInput.equals (df.format (df.parse (sInput)))
res260: Boolean = false
I hope you aren't bound to regex, and can use it.