How to validate a date field in jsp?

Hai i am developing a web application in jsp.In this at a particular field the user enters the date. Now i need to validate this date field. The date should be in the yyyy-mm-dd format. So i need to check whether the particular data entered in the text box is of the required date format. Can anyone help me in this regard by giving suggestions or a sample code.

Thank you,

[386 byte] By [autoa] at [2007-10-1 22:40:30]
# 1

You can use JavaServer Faces technology. Check MyFaces Implementation of Apache SF. They have a validateRegExpr tag to validate user inputs.

Sample Code:

<h:inputText id="regExprValue"

value="#{validateForm.regExpr}"

required="true">

<t:validateRegExpr pattern='\d{5}'/>

</h:inputText>

Example validates if the input is 5 digits. You can write your own pattern using Regular Expression. Check: www.myfaces.org

kizilalia at 2007-7-13 14:42:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for your suggestion.I will be going that way.But is there any other way where we can validate the date in jsp itself without going to these jsf and apache sf.

I mean can i use any regular expression directly in the java script to do the validation and check whether it is in yyyy-mm-dd format.

Thank you,

autoa at 2007-7-13 14:42:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

I think it is possible to use java script for validation. But do you really want it? What if, client sets his/her browser to not support Java Script? Then your database may become a mass and your application can generate exception.

Another way, beside JSF, you can use Expression Language. Take input from user, send it to your validation bean, return result. Check if it is validated.

Best wishes...

kizilalia at 2007-7-13 14:42:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi, i had tried in the way you suggested but couldn't get it right. Also i dont want much complexity to be added to this validation purpose.So i just want to do it in the java script and i think there will be no problems with the browser settings.

What i need is i just want to validate whether the date entered into that textbox is in yyyy-mm-dd format. Also in the mysql database i am using the date will be entered in the yyyy-mm-dd format and i saw somewhere in the forum that while validating in javascript it is not possible to give in yyyy-mm-dd format i.e., it doesnot identify the hyphen and only slash needs to be given. So what should i do for this validation to be perfect.This should be simple with little complexity and accurate.Can anyone give me the suggestion regarding it. or if possible the sample code.I need it very urgent.

autoa at 2007-7-13 14:42:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

This may not be the best way to do it, but hey, it works for me:

public java.util.Date isValidDate(String sDateIn) {

SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd");

sdfYMD.setLenient(false);

ParsePosition pos = new ParsePosition(0);

java.util.Date dDate = sdfYMD.parse(sDateIn, pos);

return dDate;

}

HLamlina at 2007-7-13 14:42:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
how to include java script for validating date field which is used in jsp page...i need java script code for validating date field to be included in jsp pagethank u vimala.v.o
vmlsri@yahoo.coma at 2007-7-13 14:42:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
HLamlin..You need to add the try {..} catch, if sDateIn contain invalid date, this procedure will return an error.btw..it's cool..thx
rizki.antoa at 2007-7-13 14:42:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

I am developing a web application in jsp. I need to validate a date field. Below is the HTML of the date field that I would like to validate. Can anyone help me in this regard by giving suggestions or a sample code.

<TD Width="40%"><B><input type="text" name="txtBeginDate" VALUE="" ></TD>

boylebravea at 2007-7-13 14:42:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...