Click here to Skip to main content
15,917,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi everyone,
Can anyone show how to write the java script code for start date and end date validation ....
Posted

Validating what? What is the format you want to accept? What is the semantic of acceptable values - besides the obvious, that end date has to be greater than start date?

But you can start out from this one:

JavaScript
<script>
function IsValidDate(date){
    var D,M,Y;
    var re= /^(\d{4})-(\d{1,2})-(\d{1,2})$/; //YYYY-M-D, YYYY-MM-D, YYYY-M-DD, YYYY-MM-DD
    
	var match = date.match(re);
    if(!match) return false;
    
    Y = 1*match[1];
    M = 1*match[2];
    D = 1*match[3];

    var dt = new Date(Y,M-1,D);
 
    if(dt.getDate()!=D){
        return(false);
    }
    else if(dt.getMonth()!=M-1){
        return(false);
    }
    else if(dt.getFullYear()!=Y){
        return(false);
    }
        
    return(dt);
 }

function ValidateDateInput(id) {
var result = IsValidDate(document.getElementById(id).value);
if (result) 
	document.getElementById(id).style.color="green";	
else
   document.getElementById(id).style.color="red";
return result;
}
 
function validate(){
  var date1 = ValidateDateInput("date_1");
  var date2 = ValidateDateInput("date_2");
  if (date1 && date2 && date1 < date2 )
	  document.getElementById("dates").style.backgroundColor="green";	
  else
      document.getElementById("dates").style.backgroundColor="red";	
} 
 
</script>
<div id="dates">
Date1: <input type="text" id="date_1" name="date_1" value="2012-02-10">
Date2: <input type="text" id="date_2" name="date_2" value="2012-02-12">
<button onclick="validate();">Let's see</button>
</div>
 
Share this answer
 
Hi i think it's helpfull to you



SQL
function checkDOJ(sender, args) {
               if (sender._selectedDate < new Date()) {
                   alert("You cannot select a day earlier than today!");
                   sender._selectedDate = new Date();
                   // set the date back to the current date
                   sender._textbox.set_Value(sender._selectedDate.format(sender._format))
               }
           }

           function checkDOB(sender, args) {
               if (sender._selectedDate > new Date()) {
                   alert("You cannot select a day Greater than today!");
                   sender._selectedDate = new Date();
                   // set the date back to the current date
                   sender._textbox.set_Value(sender._selectedDate.format(sender._format))
               }


Regards...........

Balaji
www.leaderbalaji.blogspot.com
 
Share this answer
 
Comments
GowthamVenkatesan 25-Sep-12 7:34am    
thanks for your replay, for my question, i just a beginer,so i dont know how to call this .js function through html... so pls show how to do html code for this corresponding .js file

regards,
Gowtham.V

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