I want a date field to be validated and this is my code:
It has From date & To date:
But this code accepts every characters on the keyboard and gives a popup "error" accepting the characters...
It also gives error even when proper date is entered..
For eg if I enter 1, it doesnot wait for me to enter next no. and displays error while accepting 1. It does this with every character.
I cant figure out what is wrong in the code.
It has From date & To date:
Code:
<script>function checkdate(frmdt,todt){
var validformat=/^\d{2}\-\d{2}\-\d{4}$/
var returnval=false
if(!validformat.test(frmdt.value)){
alert("Invalid frmdt");
document.form.frmdt.value="";
}
else if(!validformat.test(todt.value)){
alert("Invalid Date 2");
document.form.todt.value="";
}
else{
var start = document.form.frmdt.value;
var end = document.form.todt.value;
var stDate = new Date(start);
var enDate = new Date(end);
var compDate = enDate - stDate;
if(compDate >= 0)
return true;
else
{
alert("End date should be greater than start date.");
return false;
}
}
}</script>
It also gives error even when proper date is entered..
For eg if I enter 1, it doesnot wait for me to enter next no. and displays error while accepting 1. It does this with every character.
I cant figure out what is wrong in the code.
Comment