Validation For Date
I have one web application, in this i have to validate one date field of format like mm/dd/yyyy. I searched in the net but i didn't get the proper one. Please help me by providing
Solution 1:
Have a look at http://www.smartwebby.com/DHTML/date_validation.asp, first search result when googling for "javascript date validation"..
Solution 2:
Here's the regex you want.
var re = /^(0[1-9]|1[0-2])\/(0[1-9]|[1-3]\d)\/((19|20)\d\d)$/
Though you are probably better off, as inkedmn suggests validating by parsing the input, since MM/dd/yyyy is a recognized date format via Date.parse.
Solution 3:
I'd just try parsing the string as a Date object and check the result (assuming you only need to know if it's a valid date or not):
var myDate = Date.parse(form1.txtDateOfOccurance.value);
if(isNaN(myDate)){
// it's not a real date
}
Solution 4:
functionIsValidDate(str) {
var str2="";
var date = newDate(str);
str2 = (date.getMonth()+1) + "/"
+ date.getDay() + "/"
+ (date.getYear()+1900);
return (str == str2);
}
Solution 5:
You can use:
functioncheckdate(input){
var validformat=/^\d{2}\/\d{2}\/\d{4}$///Basic check for format validityvar returnval=false;
if (!validformat.test(input.value))
alert("Invalid Date Format. Please correct and submit again.");
else{ //Detailed check for valid date rangesvar monthfield=input.value.split("/")[0];
var dayfield=input.value.split("/")[1];
var yearfield=input.value.split("/")[2];
var dayobj = newDate(yearfield, monthfield-1, dayfield);
if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
alert("Invalid Day, Month, or Year range detected. Please correct and submit again.");
else
returnval=true;
}
if (returnval==false) input.select()
return returnval;
}
Post a Comment for "Validation For Date"