ASP.NET - how to validate a textbox for DateOFBirth

Asked By aman on 25-Oct-11 02:25 AM
hi all,
thanks for your previous help.......

i have a textbox and want to validate it .......the textbox is for DateOfBirth.

please suggest me with some example.

thanks
smr replied to aman on 25-Oct-11 02:26 AM
HI

try this
function dateValidation()
 
{
var obj = document.getElementById("<%=txtDate.ClientID%>");
 
var day = obj.value.split("/")[0];
var month = obj.value.split("/")[1];
 
var year = obj.value.split("/")[2];
if ((day<1 || day >31) || (month<1&&month>12)&&(year.length != 4))
 
{
alert("Invalid Format");return false;
 
}
 
else
 
{
var dt = new Date(year, month-1, day);
 
var today = new Date();
if((dt.getDate() != day) || (dt.getMonth() != month-1) || (dt.getFullYear()!=year) || (dt>today))
 
{
alert("Invalid Date");return false;
 
}
 
}
 
}
smr replied to aman on 25-Oct-11 02:28 AM
hi

try this also

<html>
<head>
<script language="javascript">
function checkDt(fld) {
  var mo, day, yr;
  var entry = fld.value;
  var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
  var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
  var valid = (reLong.test(entry)) || (reShort.test(entry));
  if (valid) {
    var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
    var delim1 = entry.indexOf(delimChar);
    var delim2 = entry.lastIndexOf(delimChar);
    mo = parseInt(entry.substring(0, delim1), 10);
    day = parseInt(entry.substring(delim1+1, delim2), 10);
    yr = parseInt(entry.substring(delim2+1), 10);
    // handle two-digit year
    if (yr < 100) {
      var today = new Date();
      // get current century floor (e.g., 2000)
      var currCent = parseInt(today.getFullYear() / 100) * 100;
      // two digits up to this year + 15 expands to current century
      var threshold = (today.getFullYear() + 15) - currCent;
      if (yr > threshold) {
        yr += currCent - 100;
      } else {
        yr += currCent;
      }
    }
    var testDate = new Date(yr, mo-1, day);
    if (testDate.getDate() == day) {
      if (testDate.getMonth() + 1 == mo) {
        if (testDate.getFullYear() == yr) {
          // fill field with database-friendly format
          fld.value = mo + "/" + day + "/" + yr;
          return true;
        } else {
          alert("Check the year entry.");
        }
      } else {
        alert("Check the month entry.");
      }
    } else {
      alert("Check the date entry.");
    }
  } else {
    alert("Invalid date format. Enter as mm/dd/yyyy.");
  }
  return false;
}
 
</script>
</head>
<body>
<input type="text" id="txtDt" onblur="checkDt(this)" />
</body>

follow http://forums.asp.net/t/1357242.aspx
Riley K replied to aman on 25-Oct-11 02:31 AM


What kind of validation you want, do you just want a valid date
??

to check if it is valid date do like this

var date = "01/10/1981";

var parsedDate = new DateTime();
if (DateTime.TryParse(date, out parsedDate))
{
    // Date is valid and is now in "date"
}
else
{
    // Date is NOT valid / parseable
}
Jitendra Faye replied to aman on 25-Oct-11 02:32 AM
Validate like this-

Markup:

<asp:Textbox runat=

"server" ID="TextBox1" />

<asp:CustomValidator runat="server" ControlToValidate="TextBox1" ErrorMessage="Date was in incorrect format" OnServerValidate="CustomValidator1_ServerValidate" />


Code-behind:


protected

void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)

{


DateTime d;

e.IsValid = DateTime.TryParseExact(e.Value, "dd/MM/yyyy", CultureInfo.InvarinatCulture, DateTimeStyles.None, out d);

}


if

you want to allow several formats and only them, use next:

DateTime

.TryParseExact(e.Value, new[] { "dd/MM/yyyy", "yyyy-MM-dd" }, CultureInfo.InvarinatCulture, DateTimeStyles.None, out d);

 


Try this and let me know.
aneesa replied to aman on 25-Oct-11 02:36 AM
Use this function to get desired result. yyyy-mm-dd
 
function validateURL(id){
var elementValue=document .getElementById(id).value;
var urlregex = new RegExp(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/));
if(!urlregex.test(elementValue))
{
alert('The string was not recognized as a valid DateTime');
 
}
else
{
return true;
}
}
dipa ahuja replied to aman on 25-Oct-11 02:37 AM
You can use the range validator

Birth Date:<br />
<asp:TextBox id="birthDateTextBox" runat="server" />

<asp:RangeValidator id="birthDateRangeTest" runat="server"
Type="Date" ControlToValidate="birthDateTextBox" 
MinimumValue="1/1/1970" 
MaximumValue="12/31/1990"
ErrorMessage="You must've been born in year 1970 to 1990!" />
aneesa replied to aman on 25-Oct-11 02:37 AM

And you can use regularexpressionvalidator validation control to validate the textbox.
 
<asp:regularexpressionvalidator ID="Regularexpressionvalidator1" runat="server"  errormessage="RegularExpressionValidator" ControlToValidate="txtbox1" ValidationExpression="^(([1-9])|(0[1-9])|(1[0-2]))\/((0[1-9])|([1-31]))\/((\d{2})|(\d{4}))$"></asp:regularexpressionvalidator>

Sri K replied to aman on 27-Oct-11 02:55 AM

You need to validate the Date of Birth given below
Markup:

<asp:Textbox runat="server" ID="TextBox1" />
<asp:CustomValidator runat="server" ControlToValidate="TextBox1" ErrorMessage="Date was in incorrect format" OnServerValidate="CustomValidator1_ServerValidate" />

Code-behind:

protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
  DateTime d;
  e.IsValid = DateTime.TryParseExact(e.Value, "dd/MM/yyyy", CultureInfo.InvarinatCulture, DateTimeStyles.None, out d);
}

if you want to allow several formats and only them, use next:

DateTime.TryParseExact(e.Value, new[] { "dd/MM/yyyy", "yyyy-MM-dd" }, CultureInfo.InvarinatCulture, DateTimeStyles.None, out d);