Showing posts with label Date validation. Show all posts
Showing posts with label Date validation. Show all posts

Sunday, March 27, 2022

Validate dd/mm/yyyy on input keyup jQuery

 Format: dd/mm/yyyy

$(document).on("keyup", ".input-date-only", function () {  
   var inputVal = $(this).val();  
   let valid = /^0?$|^(?:0?[1-9]|[12]\d|3[01])(?:\/(?:(?:0$|0?[1-9]|1[012]?)(?:\/\d{0,4})?)?)?$/.test(inputVal), input = inputVal;  
   if (!valid) {  
     alert("Please enter a valid date!");  
     $(this).val(input.substring(0, input.length - 1));  
   }  
 });  

Use the below regex for checking the other formats

 Format: dd-mm-yyyy

^0?$|^(?:0?[1-9]|[12]\d|3[01])(?:-(?:(?:0$|0?[1-9]|1[012]?)(?:-\d{0,4})?)?)?$ 

 Format: mm-dd-yyyy

^0?$|^(?:0?[1-9]|1[012]?)(?:-(?:(?:0$|0?[1-9]|[12]\d|3[01])(?:-\d{0,4})?)?)?$  

 Format: yyyy-mm-dd

^\d{0,4}$|^\d{4}-0?$|^\d{4}-(?:0?[1-9]|1[012])(?:-(?:0?[1-9]?|[12]\d|3[01])?)?$ 

Wednesday, June 7, 2017

Function for validate dd/MM/yyyy date using jQuery

 function VaidateDate(dateValue)  
   {  
     var dateRegex = new RegExp(/^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-.\/])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/);  
     return dateRegex.test(dateValue);  
   }  

If function return true then it is valid else it is invalid.

Tuesday, March 14, 2017

Validating date format using java script


 // Validates that the input string is a valid date formatted as "mm/dd/yyyy"  
 function isValidDate(dateString)  
 {  
   // First check for the pattern  
   if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))  
     return false;  
   // Parse the date parts to integers  
   var parts = dateString.split("/");  
   var day = parseInt(parts[1], 10);  
   var month = parseInt(parts[0], 10);  
   var year = parseInt(parts[2], 10);  
   // Check the ranges of month and year  
   if(year < 1000 || year > 3000 || month == 0 || month > 12)  
     return false;  
   var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];  
   // Adjust for leap years  
   if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))  
     monthLength[1] = 29;  
   // Check the range of the day  
   return day > 0 && day <= monthLength[month - 1];  
 };