nisarkhan
asked on
validate date
i have been pounding my head on the wall......
how do you validate a date?
here is what i'm using for regex its working great but its failing when i enter "09/09/009"
here is the code:
<asp:RegularExpressionVali dator ID="RegularExpressionValid ator2" runat="server" ControlToValidate="txtStar tDate"
ValidationExpression="^((0 ?[13578]|1 0|12)(-|\/ )(([1-9])| (0[1-9])|( [12])([0-9 ]?)|(3[01] ?))(-|\/)( (19)([2-9] )(\d{1})|( 20)([01])( \d{1})|([8 901])(\d{1 }))|(0?[24 69]|11)(-| \/)(([1-9] )|(0[1-9]) |([12])([0 -9]?)|(3[0 ]?))(-|\/) ((19)([2-9 ])(\d{1})| (20)([01]) (\d{1})|([ 8901])(\d{ 1})))$"
Text="*" ErrorMessage="Invalid Start Date" Display="Dynamic"></asp:Re gularExpre ssionValid ator>
i have the same question here but no help and none of those solution works for me
ps: please read my comments and also
if i enter "09/09/009" the regex thinks its a valid date. actually its not.
how do you validate a date?
here is what i'm using for regex its working great but its failing when i enter "09/09/009"
here is the code:
<asp:RegularExpressionVali
ValidationExpression="^((0
Text="*" ErrorMessage="Invalid Start Date" Display="Dynamic"></asp:Re
i have the same question here but no help and none of those solution works for me
ps: please read my comments and also
if i enter "09/09/009" the regex thinks its a valid date. actually its not.
ASKER
very frustration with date validation
is that client validation? can you please show me how i will be wiring?
thanks.
is that client validation? can you please show me how i will be wiring?
thanks.
That'll make sure a user enters only in date format.
Just have to put this in the onblur of the textbox that holds the date. For example
If you textbox is as,
<asp:Textbox ID="txtDate" runat="server"></asp:Textb ox> then
In the page_load use an attribute.add as,
txtDate.Attributes.Add("da teType", "US");
txtDate.Attributes.Add("on blur", "if(!fnEvaluateDate(this)) this.focus ();"
If you want to use any different format, then just add the format in the fnGetDateObj function above as I did for US and the value of the add attribute dateType should be that value. Hope this helps
If you textbox is as,
<asp:Textbox ID="txtDate" runat="server"></asp:Textb
In the page_load use an attribute.add as,
txtDate.Attributes.Add("da
txtDate.Attributes.Add("on
If you want to use any different format, then just add the format in the fnGetDateObj function above as I did for US and the value of the add attribute dateType should be that value. Hope this helps
ASKER
nmarun:: no third party tool
CB_Thirumalai:: i tried to use your code but does not work
here is the what i'm doing.
CB_Thirumalai:: i tried to use your code but does not work
here is the what i'm doing.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function fnEvaluateDate(obj){
var bRetValue=true;
if(obj.value!=""){
var dateObj=fnGetDateObj(obj);
var day=dateObj[2],month=dateObj[1],year=dateObj[0];
var theDate=new Date(year,month-1,day);
if(theDate.getMonth()+1!=month || theDate.getDate()!=day || theDate.getFullYear()!=year)
bRetValue=false;
}
return bRetValue;
}
// Get Date Object (Always send object as {year,month,day})
function fnGetDateObj(obj){
var retVal,actVal;
switch(obj.dateType){
case 'US':
actVal=obj.value.split("/");
retVal=new Array(actVal[2],actVal[0],actVal[1]);
break;
}
return retVal;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Textbox ID="txtDate" runat="server"></asp:Textbox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="txtDate" ErrorMessage="invalid date"
ClientValidationFunction="fnEvaluateDate" Display="Dynamic"
EnableTheming="True" SetFocusOnError="True"></asp:CustomValidator>
<br />
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
txtDate.Attributes.Add("dateType", "US");
txtDate.Attributes.Add("onblur", "if(!fnEvaluateDate(this))this.focus();");
}
ASKER
i'm using MM/DD/YYYY date format
use this regex
(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01] )[- /.](19|20)[0-9]{2}
(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]
How about the native Calendar control?
ASKER
PraveenVenu:
no luck and its very strange ... if i enter the date like "02/02/1989" i get * (star) next to the txtDate textbox but in reality this is a valid date, any idea what i'm doing wrong?
no luck and its very strange ... if i enter the date like "02/02/1989" i get * (star) next to the txtDate textbox but in reality this is a valid date, any idea what i'm doing wrong?
<asp:TextBox runat='server' ID='txtDate' CssClass='formElementCal'></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="cal_date" TargetControlID='txtDate' runat="server" EnableViewState="true"></ajaxToolkit:CalendarExtender>
<ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender3" runat="server" TargetControlID="txtDate" Mask="99/99/9999" MessageValidatorTip="true" OnFocusCssClass="MaskedEditFocus" OnInvalidCssClass="MaskedEditError" MaskType="Date" DisplayMoney="Left" AcceptNegative="Left" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtDate"
ValidationExpression="(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}"
Text="*" ErrorMessage="Invalid Visit End Date" Display="Dynamic"></asp:RegularExpressionValidator>
ASKER
also if enter date like this:
21/54/5454
i get this error:
String was not recognized as a valid DateTime.
should the RegularExpressionValidator validation catch ?
21/54/5454
i get this error:
String was not recognized as a valid DateTime.
should the RegularExpressionValidator
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i have not tested this solution, for the sake of closing this question i'm accepting this as answers
Open in new window