validate date to mm/dd/yyyy format in javascript
Here's the snippet you need. All you need to pass date to below function which returns true/false if the given date is valid/invalid
function validateDate(dateValue)
{
var selectedDate = dateValue;
if(selectedDate == '')
return false;
var regExp = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; //Declare Regex
var dateArray = selectedDate.match(regExp); // is format OK?
if (dateArray == null){
return false;
}
month = dateArray[1];
day= dateArray[3];
year = dateArray[5];
if (month < 1 || month > 12){
return false;
}else if (day < 1 || day> 31){
return false;
}else if ((month==4 || month==6 || month==9 || month==11) && day ==31){
return false;
}else if (month == 2){
var isLeapYear = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day> 29 || (day ==29 && !isLeapYear)){
return false
}
}
return true;
}
The above function will:
- Checks for proper date format as MM/DD/YYYY.
- Checks whether the given date is valid or not. Ex: April month is having only 30 days. If we specify day as 31 for the month of April then this function will validate it as invalid date.
- Checks for 29th day of February. It will validate as a valid date only if the specified year is a leap year.
For more info please go through the link: http://www.j2eekart.com/2015/01/date-validation-in-javascript.html
Change your code as:
<script type="text/javascript">
$(document).ready(function(){
$('.datepicker').datepicker();
$(".datepicker").on("blur", function (e){
var isValidDate = validateDate(e.target.value);
if(!isValidDate){
alert("Please enter a valid date in MM/DD/YYYY format");
}
});
});
function validateDate(dateValue)
{
var selectedDate = dateValue;
if(selectedDate == '')
return false;
var regExp = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; //Declare Regex
var dateArray = selectedDate.match(regExp); // is format OK?
if (dateArray == null){
return false;
}
month = dateArray[1];
day= dateArray[3];
year = dateArray[5];
if (month < 1 || month > 12){
return false;
}else if (day < 1 || day> 31){
return false;
}else if ((month==4 || month==6 || month==9 || month==11) && day ==31){
return false;
}else if (month == 2){
var isLeapYear = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day> 29 || (day ==29 && !isLeapYear)){
return false
}
}
return true;
}
</script>
Regex - Date (mm/dd/yyyy) Formatting (JavaScript)
You got it incorrect. This shouldn't be done via Regex and in fact, it is not meant to do that. In order to that, you need to take the following steps:
1- Check the pattern via Regex
2- Validate the date.
The pattern you are looking for is:
var pattern = /(0\d{1}|1[0-2])\/([0-2]\d{1}|3[0-1])\/(19|20)\d{2}/
Take a look at this to see the pattern: http://regexr.com/3fe3s
Finally, if you want to check whether the date is a valid date or not (after checking in the pattern).
Here is the entire method:
function isValidDate(dtValue2) {
// your desired pattern
var pattern = /(0\d{1}|1[0-2])\/([0-2]\d{1}|3[0-1])\/(19|20)(\d{2})/
var m = dtValue2.match(pattern);
if (!m)
return false;
var d = new Date(dtValue2);
// Now let's ensure that the date is not out of index
if (d.getMonth()+1 == parseInt(m[1], 10) && d.getDate() ==parseInt(m[2], 10)) {
return true;
}
return false;
}
regular expression to validate datetime format (MM/DD/YYYY)
Try your regex with a tool like http://jsregex.com/ (There is many) or better, a unit test.
For a native validation:
function validateDate(testdate) {
var date_regex = /^\d{2}\/\d{2}\/\d{4}$/ ;
return date_regex.test(testdate);
}
In your case, to validate (MM/DD/YYYY), with a year between 1900 and 2099, I'll write it like that:
function validateDate(testdate) {
var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
return date_regex.test(testdate);
}
Regular expression for date format MM/DD/YYYY returns false results
Please correct your regex (valid date example: 03/31/1988):
$(document).ready(function(){
$("#submitform").click(function(){
/* isValidData source: http://stackoverflow.com/questions/5812220/how-to-validate-a-date */
function isValidDate(s) {
var bits = s.split('/');
var d = new Date(bits[2], bits[0] - 1, bits[1]);
return d && (d.getMonth() + 1) == bits[0] && d.getDate() == Number(bits[1]);
}
var daterequested = $("#prfm_dtrequest").val();
var dateMMDDYYYRegex = "^[0-9]{2}/[0-9]{2}/[0-9]{4}$";
if(daterequested.match(dateMMDDYYYRegex) && isValidDate(daterequested)){
alert('is a valid date: '+daterequested);
}else{
alert('is not a valid date: '+daterequested);
}
});
});
Demo
Result:
Related Topics
How to Open New Tab in JavaScript Without Switching to the New Tab
Convert HTML Table to Array in JavaScript
How to Mute an HTML5 Video Player Using Jquery
Check If HTML Snippet Is Valid with JavaScript
Html/Javascript: Simple Form Validation on Submit
Selecting Multiple from an HTML Select Element Without Using Ctrl Key
Show a Second Dropdown Based on Previous Dropdown Selection
Scale Element Proportional to Background Cover with Jquery
Reading from Udp Port in Browser
"Pointer-Events: None" Does Not Work in IE9 and Ie10
How to Change Only Text Node in Element
Get Value of a Custom Attribute
How to Change HTML Object Element Data Attribute Value in JavaScript
JavaScript - Dynamically Assign Onclick Event in the Loop
CSS Height 100% Percent Not Working