473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 27884
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

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

Similar topics

2
by: trusst_3 | last post by:
Hello, This is the only line in my entire code that does not validate in XHTML : <td width="24" bgcolor="#330066" onclick="javascript:color1()" class="borderWidthr"></td> However, it is...
0
by: ED M. | last post by:
Hello all...I'm new to the board. I have a problem that I hope someone here might be able to solve for me. I am doing some clientside validation using Javascript. The text I am testing is...
1
by: C | last post by:
Hi, I have two input fields on an ASP Page. User enters 2 dates. I want to check that the difference in months does not exceed 360 months. Is this possible in Javascript to get the...
2
by: ramesh_tankala | last post by:
hi... How to handle date using Maskedit(Correect date)
1
by: etsomik | last post by:
Hi guys. I need some help in javascript. I wrote a function which supposed to validate the date. And it does working it addin slashes as you type that what I want, However now I need to display a...
2
by: nbt725 | last post by:
Dear Sir, Hello ! I need to validate my login form which is displayed using <div> to give sliding effect and not to refresh page, hence can't use generic php submit but to validate using...
6
by: lucoin | last post by:
Hello guys, I met a problem about php and javascipt For a tickets booking system, when a customer search for flights from one city, so in the data base there should be many routes to different...
1
by: printline | last post by:
Hello All I'm a newbee to javascript/ajax. I have produced a form, where i want to do some validation on some fields. I have used the spry framework and it works fine. Now, i have a select...
1
by: scott | last post by:
Hello, Thanks in advance for any help you might be able to offer... I have an html table with two columns and 10 rows. The first column contains a textbox that is populated. The second column...
9
mageswar005
by: mageswar005 | last post by:
Hi Guys, My array textbox validation is not working fine,my code is given below please help me immediately. function validation() { var chks =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.