Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HEllo Team,
I have done the validation for DateTime in javascript as follow:

C#
function isDate(txtDate) {
               var currVal = txtDate;
               if (currVal == '')
                   return false;

               //Declare Regex
               var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
               var dtArray = currVal.match(rxDatePattern); // is format OK?

               if (dtArray == null)
                   return false;

               //Checks for mm/dd/yyyy format.
               dtDay = dtArray[1];
               dtMonth = dtArray[3];
               dtYear = dtArray[5];

               if (dtMonth < 1 || dtMonth > 12)
                   return false;
               else if (dtDay < 1 || dtDay > 31)
                   return false;
               else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31)
                   return false;
               else if (dtMonth == 2) {
                   var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
                   if (dtDay > 29 || (dtDay == 29 && !isleap))
                       return false;
               }


and i am validating in this manner
C#
if ($('#txtLoanDate').val() != '' && !isDate($('#txtLoanDate').val())) {
                    alert("EMI date must be in DD/MM/YY Format ");
                    return false;
                }


My problem is when i am sending the blank value(no Date) in txtLoanDate it says the error
String was not recognized as a valid DateTime in CodeBehind page.

How should i get solve??
Please help it

Thanks
Harshal Raut
Posted
Updated 27-Jan-14 22:04pm
v2

yes,but if you use required field validation then also you will be able to show error message , in fact validator itself will show message, just you have to customize its error message.
 
Share this answer
 
Comments
[no name] 28-Jan-14 4:43am    
Thank you very much.I Got the idea Bro..
You can use required field validation ,so that it will prevent blank date value submission.
 
Share this answer
 
Comments
[no name] 28-Jan-14 4:25am    
Thanks for your quick reply...
but if it is Blank i want to alert("DateTime Cannot be Blank").its not a Mandatory.
Then What to do??
[no name] 28-Jan-14 4:29am    
I have the form with image .As the Image get load on page the record get loaded on particula textbox for example:Datetime vale get show in DateTime textbox.My Company requirement is suppose if DatTime is empty then alert (enter the date) and if != "" then check the validation format.
after that i need to put the date in DateTimeTextbox..Thats my Question..??
V5709 28-Jan-14 4:49am    
ok.. then you can show alert message using java script from code behind also.Check for blank values during assigning values to controls & if blank then show error alert from code behind. For this check "http://stackoverflow.com/questions/5825165/javascript-alert-showmessage-from-asp-net-code-behind" .
Hope this will help you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900