How to Validate Date with Format "Mm/Dd/Yyyy" in JavaScript

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>

Validate date in both 'mm-dd-yyyy' and 'mm/dd/yyyy' formats using JQuery Validate

You could put in an array the date formats that you support and loop on that. For example with a function like this:

function isValidDate(input) {
var regexes = [
/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/,
/^(\d{1,2})\-(\d{1,2})\-(\d{4})$/
];

for (var i = 0; i < regexes.length; i++) {
var r = regexes[i];
if(!r.test(input)) {
continue;
}
var a = input.match(r), d = new Date(a[3],a[1] - 1,a[2]);
if(d.getFullYear() != a[3] || d.getMonth() + 1 != a[1] || d.getDate() != a[2]) {
continue;
}
// All checks passed:
return true;
}

return false;
}

Then you call it from your code like so:

$(document).on('blur','.dateValidation',function(){ 
var isValid = isValidDate($(this).val());
if (isValid) {
alert('Valid date');
} else {
alert('Invalid date');
}
});

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);
}


Related Topics



Leave a reply



Submit