By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

Date entry way

vikas251074
100+
P: 198
Can anyone give me way to enter date in dd-mm-yyyy format with complete verification?

Thanks and regards,
Vikas
Jun 23 '08 #1
Share this Question
Share on Google+
7 Replies


hsriat
Expert 100+
P: 1,654
Do you want to use regular expression to validate it?...

then use /^[0-3][0-9]\-[0-1][0-9]\-[0-9][0-9]$/
Jun 23 '08 #2

vikas251074
100+
P: 198
No sir,
I only want to validate the date like 30th feb, 31st april and so on.

Thanks and regards,
Vikas
Jun 23 '08 #3

gits
Expert Mod 5K+
P: 5,390
since your 'verification'-requirement is too vague please explain in more detail what you want to verify. give an example for the input and what the output should be ... so should the input be transformed or should it just throw an error or what? date-inputs could also be improved with using date-widgets that just allow the correct input ... you may use seperated input fields (day, month, year) or drop-downs etc. ...

kind regards
Jun 23 '08 #4

vikas251074
100+
P: 198
since your 'verification'-requirement is too vague please explain in more detail what you want to verify. give an example for the input and what the output should be ... so should the input be transformed or should it just throw an error or what? date-inputs could also be improved with using date-widgets that just allow the correct input ... you may use seperated input fields (day, month, year) or drop-downs etc. ...

kind regards
<input type="text" name="birth_date"/>

if user enter '30-02-1985' or 31-04-1985' or '29-02-2007' then gives error message 'Invalid date'.
Jun 23 '08 #5

hsriat
Expert 100+
P: 1,654
I'd say you better use a calender component (google for DHTML JavaScript calender). But if you still need to do validation like you said, you can make your own small functions. Let one of them be:
Expand|Select|Wrap|Line Numbers
  1. function numOfDays(month,year) {
  2.     if (month<1 || month>12) return false;
  3.     switch(month) {
  4.         case 2:
  5.             if (year%4==0) return 29;
  6.             return 28;
  7.         case 4:
  8.         case 6:
  9.         case 9:
  10.         case 11:
  11.             return 30;
  12.         default:
  13.             return 31;
  14.     }
  15. }
If hope you can get an idea from this.
Jun 23 '08 #6

100+
P: 428
Try to make a date from the input, then see if the date's year, month and day are what was input (adjusting month for the index).Avoids April 40 being silently converted to the valid May 10, and so on.

You don't need the try-catch, but it can be worth having with user input.


Expand|Select|Wrap|Line Numbers
  1. Valor= {
  2.     dmy: function(str,monthfirst){
  3.         var A= str.split(/\D+/);
  4.         try{
  5.             A= monthfirst? [A[2]*1,A[0]-1,A[1]*1] : [A[2]*1,A[1]-1,A[0]*1];
  6.             var D= new Date(A[0],A[1],A[2]);
  7.             if(D.getFullYear()== A[0] && D.getMonth()== A[1] 
  8.             && D.getDate()== A[2]){
  9.                 return D;
  10.             }
  11.         }
  12.         catch(er){
  13.         }
  14.         return false;
  15.     },
  16.     mdy: function(str){
  17.         return valid_dmy(str,true);
  18.     }
  19. }
// test with any value. Any nondigits can delimit the numbers.
alert(Valor.dmy('29/2/2009'));
alert(Valor.mdy('2/29/2009'));
alert(Valor.dmy('29/2/2008'));
alert(Valor.mdy('2/29/2008'));.
Jun 23 '08 #7

100+
P: 428
// Edit- sorry, I set a bad reference in my example

Expand|Select|Wrap|Line Numbers
  1. Valor= {
  2.     dmy: function(str,monthfirst){
  3.         var A= str.split(/\D+/);
  4.         try{
  5.             A= monthfirst? [A[2]*1,A[0]-1,A[1]*1] : [A[2]*1,A[1]-1,A[0]*1];
  6.             var D= new Date(A[0],A[1],A[2]);
  7.             if(D.getFullYear()== A[0] && D.getMonth()== A[1]
  8.             && D.getDate()== A[2]){
  9.                 return D;
  10.             }
  11.         }
  12.         catch(er){
  13.         }
  14.         return false;
  15.     }
  16. }
  17. Valor.mdy=function(str){
  18.     return Valor.dmy(str,true);
  19. }
// test with any value. Any nondigits can delimit the numbers.
alert(Valor.dmy('29/2/2009'));
alert(Valor.mdy('2/29/2009'));
alert(Valor.dmy('29/2/2008'));
alert(Valor.mdy('2/29/2008'));
Jun 24 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.