Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to check validation of date format in textchanged event in text box in asp.net in front end
Posted
Comments
tanweer 31-May-12 0:53am    
hi, how it will validate a Date on textchange event even the date is not completely entered? are you asking on leave or blur event???
[no name] 31-May-12 1:02am    
@tanveer:ya m asking on leave event but in the asp.net there is no leave event for textbox...that's why i m asking on textchanged event

hi You can't use server side validation code in text change event in ASP .Net. What you can do is to use javascript to validate your text control.

You can do it on either blur or onchange event of the field.
 
Share this answer
 
You can validate just before save.
what about using a javascript drop down calendar?

Regards
Sebastian
 
Share this answer
 
v2
try like this:

JavaScript
$(document).ready(function() {
  $('#txtDate').blur(function() {
      if(validateDate('txtDate'))
      {
          alert('Date is valid');
      }
      else
      {
          alert('Invalid Date!!!');
      }
   });
});

//validation function Starts
function validateDate(txtDate){
   var txtVal = document.getElementById(txtDate).value;
   var filter = new RegExp("(0[123456789]|10|11|12)([/])([1-2][0-9][0-9][0-9])");
   if(filter.test(txtVal))
      return true;
   else
     return false;
}​
//Ends
//
 
Share this answer
 
Comments
[no name] 31-May-12 1:24am    
where it can be called?
tanweer 31-May-12 1:34am    
you dont need to call it, it will automatically call the method when the textbox leave event fires
Steps:
1. Add a onchanged client event to textbox on client side using Attributes.
2. Refer a JS function to the event
3. Pass textbox or textbox value to event method
4. Validate the data using regex or crude way

Something like:
C#
TextBox4.Attributes.Add("onchange","javascript: ValidateMe(this);");

JavaScript
<script type="text/javascript">
// Will fire as soon as textbox looses focus after a change
function ValidateMe(curr) {
   //    alert(curr.value);
   // use the value and validate it
}
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900