Date entry way

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikas251074
    New Member
    • Dec 2007
    • 198

    #1

    Date entry way

    Can anyone give me way to enter date in dd-mm-yyyy format with complete verification?

    Thanks and regards,
    Vikas
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Do you want to use regular expression to validate it?...

    then use /^[0-3][0-9]\-[0-1][0-9]\-[0-9][0-9]$/

    Comment

    • vikas251074
      New Member
      • Dec 2007
      • 198

      #3
      No sir,
      I only want to validate the date like 30th feb, 31st april and so on.

      Thanks and regards,
      Vikas

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        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

        Comment

        • vikas251074
          New Member
          • Dec 2007
          • 198

          #5
          Originally posted by gits
          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_dat e"/>

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

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            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:[CODE=javascript]function numOfDays(month ,year) {
            if (month<1 || month>12) return false;
            switch(month) {
            case 2:
            if (year%4==0) return 29;
            return 28;
            case 4:
            case 6:
            case 9:
            case 11:
            return 30;
            default:
            return 31;
            }
            }[/CODE]

            If hope you can get an idea from this.

            Comment

            • mrhoo
              Contributor
              • Jun 2006
              • 428

              #7
              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.


              Code:
              Valor= {
              	dmy: function(str,monthfirst){
              		var A= str.split(/\D+/);
              		try{
              			A= monthfirst? [A[2]*1,A[0]-1,A[1]*1] : [A[2]*1,A[1]-1,A[0]*1];
              			var D= new Date(A[0],A[1],A[2]);
              			if(D.getFullYear()== A[0] && D.getMonth()== A[1] 
              			&& D.getDate()== A[2]){
              				return D;
              			}
              		}
              		catch(er){
              		}
              		return false;
              	},
              	mdy: function(str){
              		return valid_dmy(str,true);
              	}
              }
              // 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'));.

              Comment

              • mrhoo
                Contributor
                • Jun 2006
                • 428

                #8
                // Edit- sorry, I set a bad reference in my example

                Code:
                Valor= {
                	dmy: function(str,monthfirst){
                		var A= str.split(/\D+/);
                		try{
                			A= monthfirst? [A[2]*1,A[0]-1,A[1]*1] : [A[2]*1,A[1]-1,A[0]*1];
                			var D= new Date(A[0],A[1],A[2]);
                			if(D.getFullYear()== A[0] && D.getMonth()== A[1]
                			&& D.getDate()== A[2]){
                				return D;
                			}
                		}
                		catch(er){
                		}
                		return false;
                	}
                }
                Valor.mdy=function(str){
                	return Valor.dmy(str,true);
                }
                // 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'));

                Comment

                Working...