5
Answers

unable to perform date validation using jquery

Photo of Test

Test

5y
903
1
I have a text box where i will be giving the date in text box and i need to compare that given date in text box with todays date it working for past date which is the expected reult but if i am giving future date its still showing me the validation instead of inserting the date.How can i resolve this issue
Below is my code that i have used
var currentdate = new Date();
var todayDate = (currentdate.getMonth() + 1) + "/" + currentdate.getDate() + "/" + currentdate.getFullYear();
if ($("#txtDate").val() < todayDate) //txtdate is 10/20/2019 and today date is 09/20/2019 but still i am getting validation which should not happen
{
MessageDisp('divAlertMessage', "Date Cannot Be Less Than Today's Date.", 'Error');
return false;
}

Answers (5)

3
Photo of Priyanka Jain
NA 9.6k 904.2k 5y
It is comparing a string with the date so it may not work try below code 
  1. if (new Date($("#txtDate").val()) < todayDate) //txtdate is 10/20/2019 and today date is 09/20/2019 but still i am getting validation which should not happen  
  2. {  
  3. MessageDisp('divAlertMessage'"Date Cannot Be Less Than Today's Date.", 'Error');  
  4. }  
 
Your issue may be resolved with above code 
 
1
Photo of Jaimin Shethiya
48 30.6k 602.1k 5y
Hello,

I have tried but its working fine for me.
can you please check the below screen shot for date validation and let me know you want to need anything on that.



var currentdate = new Date();
currentdate = (currentdate.getMonth() + 1) + "/" + currentdate.getDate() + "/" + currentdate.getFullYear();
if("9/21/2019" < currentdate){console.log('Given date is less than the current date.');}
0
Photo of Tamil Selvan
NA 155 7.3k 5y
Hi,
 
       try this below code using call function
 
fromdate ='09/15/2019';
todate='09/20/2019'; 
 
twodates(); 
 
public bool twodates(string fromdate, string todate, out string returnvalue, out string out_fromdate, out string out_todate)
{
DateTime sdate, edate, currentdate;
out_fromdate = "";
out_todate = "";
sdate = DateTime.Now;
edate = DateTime.Now;
if (fromdate != "")
{
sdate = DateTime.Parse(fromdate, System.Globalization.CultureInfo.CreateSpecificCulture("en-AU").DateTimeFormat);
}
if (todate != "")
{
edate = DateTime.Parse(todate, System.Globalization.CultureInfo.CreateSpecificCulture("en-AU").DateTimeFormat);
}
currentdate = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"), System.Globalization.CultureInfo.CreateSpecificCulture("en-AU").DateTimeFormat);
out_fromdate = sdate.ToString("MM/d/yyyy", System.Globalization.CultureInfo.InvariantCulture); //date format change here
out_todate = edate.ToString("MM/d/yyyy", System.Globalization.CultureInfo.InvariantCulture); //date format change here
if (fromdate.Length == 0)
{
returnvalue = "select the From date";
return false;
}
else if (todate.Length == 0)
{
returnvalue = "select the To date";
return false;
}
else if (sdate > currentdate)
{
returnvalue = "From date less or equal to today's date";
returnvalue = "FROM";
return false;
}
else if (edate > currentdate)
{
returnvalue = "To date less or equal to today's date";
returnvalue = "TO";
return false;
}
else if (sdate > edate)
{
returnvalue = "From less than or equal to To date";
return false;
}
returnvalue = "Success";
return true;
}
 
 
 
0
Photo of Sonu Gupta
558 1.9k 237k 5y
Hi, Go to below links- https://www.aspsnippets.com/Articles/jQuery-DatePicker-Validate-End-date-should-be-greater-than-Start-date.aspx Https://stackoverflow.com/questions/42713089/validate-date-of-birth-greater-than-today
0
Photo of Bidyasagar Mishra
NA 5.3k 379.5k 5y
Try the following links 
https://stackoverflow.com/questions/24380305/validate-date-in-dd-mm-yyyy-format-using-jquery-validate
http://www.jquerybyexample.net/2011/12/validate-date-using-jquery.html
https://forum.jquery.com/topic/easiest-way-to-validate-an-input-date-in-custom-format
https://forums.asp.net/t/2150810.aspx?JQuery+date+validation+for+valid+date+and+in+MM+DD+YYYY+format+only