469,332 Members | 4,497 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

how to do date validation using javascript for YYYY-MM-DD format?

Hi all,

I need to allow the date format only yyyy-mm-dd to store into mysql4 data base.

I have the validation coding for mm-dd-yyyy format. But i could not modify to convert it to validate required format.

Help me.

Advance thanks.
Dec 7 '06 #1
6 27580
Hi all,

I need to allow the date format only yyyy-mm-dd to store into mysql4 data base.

I have the validation coding for mm-dd-yyyy format. But i could not modify to convert it to validate required format.

Help me.

Advance thanks.
Dec 8 '06 #2
ronverdonk
4,258 Expert 4TB
Found this at javascriptkit.com You only have to change the format check from yyyy/mm/dd into yyyy-mm-dd.
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. /**--------------------------
  3. //* Validate Date Field script- By JavaScriptKit.com
  4. //* For this script and 100s more, visit http://www.javascriptkit.com
  5. //* This notice must stay intact for usage
  6. ---------------------------**/
  7. function checkdate(input){
  8. var validformat=/^\d{4}\/\d{2}\/\d{2}$/ //Basic check for format validity
  9. var returnval=false
  10. if (!validformat.test(input.value))
  11. alert("Invalid Date Format. Please correct and submit again.")
  12. else{ //Detailed check for valid date ranges
  13. var yearfield=input.value.split("/")[0]
  14. var monthfield=input.value.split("/")[1]
  15. var dayfield=input.value.split("/")[2]
  16. var dayobj = new Date(yearfield, monthfield-1, dayfield)
  17. if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
  18. alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
  19. else {
  20.  alert ('Correct date');
  21. returnval=true }
  22. }
  23. if (returnval==false) input.select()
  24. return returnval
  25. }
  26.  
  27. </script></head>
  28. <form onSubmit="return checkdate(this.mydate)">
  29. <input type="text" name="mydate" />
  30. <input type="submit" value="submit" /><br />
  31. <b>Valid date format:</b> yyyy/mm/dd<br />
  32. <input type="submit" value="submit" />
  33. </form>
Ronald :cool:
Dec 8 '06 #3
divyaK
1
I tried to use this code but its not working .....can any one tell the reason..
Sep 24 '07 #4
gits
5,390 Expert Mod 4TB
hi ...

could you tell the error ... or what problem you have with it?

kind regards
Sep 24 '07 #5
jonso
2
Hi all,

I need to allow the date format only yyyy-mm-dd to store into mysql4 data base.

I have the validation coding for mm-dd-yyyy format. But i could not modify to convert it to validate required format.

Help me.

Advance thanks.
Just reverse the arrangement of characters in your regular expression.As it wont be enough make sure that the value you compare to the regular expression is in the format which you want.To simplify the matter why cant you sent the code so that i can look at it and change it for you.
Nov 30 '07 #6
Just reverse the arrangement of characters in your regular expression.As it wont be enough make sure that the value you compare to the regular expression is in the format which you want.To simplify the matter why cant you sent the code so that i can look at it and change it for you.
Hii,

As per your request m sending you the code. M not able to fix it from my end.Can you please help me out?

Expand|Select|Wrap|Line Numbers
  1. /****DATE VALIDATION ****
  2. *************************/
  3.  
  4. //Declaring valid date character, minimum year and maximum year
  5. var dtCh= "-";
  6. var minYear=2005;
  7. var maxYear=2015;
  8.  
  9. function isInteger(s){
  10.     var i;
  11.     for (i = 0; i < s.length; i++){   
  12.         // Check that current character is number.
  13.         var c = s.charAt(i);
  14.         if (((c < "0") || (c > "9"))) return false;
  15.     }
  16.     // All characters are numbers.
  17.     return true;
  18. }
  19.  
  20. function stripCharsInBag(s, bag){
  21.     var i;
  22.     var returnString = "";
  23.     // Search through string's characters one by one.
  24.     // If character is not in bag, append to returnString.
  25.     for (i = 0; i < s.length; i++){   
  26.         var c = s.charAt(i);
  27.         if (bag.indexOf(c) == -1) returnString += c;
  28.     }
  29.     return returnString;
  30. }
  31.  
  32. function daysInFebruary (year){
  33.     // February has 29 days in any year evenly divisible by four,
  34.     // EXCEPT for centurial years which are not also divisible by 400.
  35.     return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
  36. }
  37. function DaysArray(n) {
  38.     for (var i = 1; i <= n; i++) {
  39.         this[i] = 31
  40.         if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
  41.         if (i==2) {this[i] = 29}
  42.    } 
  43.    return this
  44. }
  45.  
  46. function isDate(dtStr){
  47.  
  48.     var strYear=dtStr.substring(pos2+1)
  49.     var strMonth=dtStr.substring(0,pos1)
  50.     var strDay=dtStr.substring(pos1+1,pos2)
  51.     var daysInMonth = DaysArray(12)
  52.     var pos1=dtStr.indexOf(dtCh)
  53.     var pos2=dtStr.indexOf(dtCh,pos1+1)
  54.  
  55.     strYr=strYear
  56.  
  57.     if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
  58.     if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
  59.     for (var i = 1; i <= 3; i++) {
  60.         if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
  61.     }
  62.  
  63.     year=parseInt(strYr)
  64.     month=parseInt(strMonth)
  65.     day=parseInt(strDay)
  66.  
  67. if (pos1==-1 || pos2==-1){
  68.         alert("The date format should be : yyyy-mm-dd")
  69.         return false
  70.     }
  71.     if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
  72.         alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
  73.         return false
  74.     }
  75.     if (strMonth.length<1 || month<1 || month>12){
  76.         alert("Please enter a valid month")
  77.         return false
  78.     }
  79.     if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
  80.         alert("Please enter a valid day")
  81.         return false
  82.     }
  83.     if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
  84.         alert("Please enter a valid date")
  85.         return false
  86.     }
  87. return true
  88. }
  89. function ValidateDate(dt){
  90.     //var dt=`.frmSample.txtDate
  91.     if (isDate(dt.value)==false){
  92.         dt.focus()
  93.         dt.style.background = 'Yellow';
  94.         return false
  95.     }
  96.     dt.style.background = 'White';
  97.     return true
  98. }
  99.  
  100. /****END DATE VALIDATION ****
Oct 21 '08 #7

Post your reply

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

Similar topics

2 posts views Thread by trusst_3 | last post: by
1 post views Thread by C | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.