Dans cet article, JavaScript tutorial , ACC @ nk tiens ? vous guider compl?te des instructions d?taill?es et des exemples de codes JavaScript pour v?rifier la validation de la date et l'heure Cette Date/Heure de validation en JavaScript tutoriel vous fournit de nombreux cas r?els pour les validations date JavaScript et le temps , ainsi que diverses solutions possibles JavaScript
DateTime validation en JavaScript didacticiel ne pas utiliser n'importe quelle biblioth?que framework JavaScript , donc vous pouvez atteindre ces id?es JavaScript validation , les solutions JavaScript valid? et les codes JavaScript par exemple facilement ? travers plain- code JavaScript
Temps JavaScript Utile et s?lecteur de dates, Plugins pour les cadres
? jsDatePick - Un simple gratuit Javascript Date Picker
? Compte ? rebours JavaScript
- Demo
- Agrandir
- Recharger
- New window
Gratuit iPage h�bergement Web pour la premi�re ann�e MOMENT
Si vous �tes toujours � la recherche d'un fournisseur d'h�bergement Web fiable avec des tarifs abordables, pourquoi vous ne prenez pas un peu de temps pour essayer iPage, seulement avec $1.89/month, inclus $500+ Cr�dits suppl�mentaires gratuites pour le paiement de 24 mois ($45)?
Plus de 1.000.000 de clients + existisng peuvent pas avoir tort, vraiment vous n'�tes pas aussi! Plus important encore, lorsque vous enregistrez l'h�bergement web � iPage gr�ce � notre lien, nous allons �tre heureux de renvoyer un plein remboursement. C'est g�nial! Vous devriez essayer iPage h�bergement web GRATUITEMENT maintenant! Et contactez-nous pour tout ce que vous devez savoir sur iPage.
When capturing information for insertion into a database, or use in other processing, it's important to control what the user can enter. Otherwise you can end up with values in the database that have no relation to reality.
Checking for valid format
In this example, the date fields will only accept input that matches the pattern 'dd/mm/yyyy' (this could just as easily be changed to 'yyyy-mm-dd' or 'mm/dd/yyyy'). The time field will allow input starting with 'hh:mm' following by an optional 'am' or 'pm'. The fields can also be empty.
The code behind the form is as follows:
function checkForm(form) { // regular expression to match required date format re = /^\d{1,2}\/\d{1,2}\/\d{4}$/; if(form.startdate.value != '' && !form.startdate.value.match(re)) { alert("Invalid date format: " + form.startdate.value); form.startdate.focus(); return false; } if(form.enddate.value != '' && !form.enddate.value.match(re)) { alert("Invalid date format: " + form.enddate.value); form.enddate.focus(); return false; } // regular expression to match required time format re = /^\d{1,2}:\d{2}([ap]m)?$/; if(form.starttime.value != '' && !form.starttime.value.match(re)) { alert("Invalid time format: " + form.starttime.value); form.starttime.focus(); return false; } alert("All input fields have been validated!"); return true; }
For each field in the form (first the dates, then the time field), a check is made as to whether the input is blank. If not, the input is compared to the regular expression. The expressions use a pre-defined class \d which represents any numeric character (0-9).
If you wanted to be really finicky the regular expression to match a date could also be written as:
re = /^[0-3]?[0-9]\/[01]?[0-9]\/[12][90][0-9][0-9]$/
If the input doesn't match the regular expression then an error message is presented, the routine stops the form from submitting by returning a false value and the focus is moved to the relevant form field.
If all tests are passed, then a value of true is returned which enables the form to be submitted.
Note: The routine does not check that the date or time input is valid, just that it matches the required format.
Checking for valid input values
Once you're in control of the input format, it's a lot easier to check that the values are actually valid. The function has been improved now so that the day, month and year values are checked to ensure that they're in the right ball-bark (ie. 1-31 for the day and 1-12 for the month). Also the year must be between 1902 and the current year.
The year limitation would be used if you were asking for a date of birth or date of some recent event. If you're setting up a calendar of future events you would check that the year is the current year or greater.
The code behind the form now is as follows:
function checkForm(form) { // regular expression to match required date format re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/; if(form.startdate.value != '') { if(regs = form.startdate.value.match(re)) { if(regs[1] < 1 || regs[1] > 31) { alert("Invalid value for day: " + regs[1]); form.startdate.focus(); return false; } if(regs[2] < 1 || regs[2] > 12) { alert("Invalid value for month: " + regs[2]); form.startdate.focus(); return false; } if(regs[3] < 1902 || regs[3] > (new Date()).getFullYear()) { alert("Invalid value for year: " + regs[3] + " - must be between 1902 and " + (new Date()).getFullYear()); form.startdate.focus(); return false; } } else { alert("Invalid date format: " + form.startdate.value); form.startdate.focus(); return false; } } if(form.enddate.value != '') { if(regs = form.enddate.value.match(re)) { if(regs[1] < 1 || regs[1] > 31) { alert("Invalid value for day: " + regs[1]); form.enddate.focus(); return false; } if(regs[2] < 1 || regs[2] > 12) { alert("Invalid value for month: " + regs[2]); form.enddate.focus(); return false; } if(regs[3] < 1902 || regs[3] > (new Date()).getFullYear()) { alert("Invalid value for year: " + regs[3] + " - must be between 1902 and " + (new Date()).getFullYear()); form.enddate.focus(); return false; } } else { alert("Invalid date format: " + form.enddate.value); form.enddate.focus(); return false; } } // regular expression to match required time format re = /^(\d{1,2}):(\d{2})([ap]m)?$/; if(form.starttime.value != '') { if(regs = form.starttime.value.match(re)) { if(regs[3]) { if(regs[1] < 1 || regs[1] > 12) { alert("Invalid value for hours: " + regs[1]); form.starttime.focus(); return false; } } else { if(regs[1] > 23) { alert("Invalid value for hours: " + regs[1]); form.starttime.focus(); return false; } } if(regs[2] > 59) { alert("Invalid value for minutes: " + regs[2]); form.starttime.focus(); return false; } } else { alert("Invalid time format: " + form.starttime.value); form.starttime.focus(); return false; } } alert("All input fields have been validated!"); return true; }
If you're not already familiar with regular expressions, then this might be getting a bit complicated. Basically, for each of the regular expression tests, an array is returned holding each component of the pattern that we've matched.
For example, when the date is checked, the return value, regs, is an array with elements 1 through 3 containing the day, month and year components of the input string. For the time check, the array returned includes the hour (pos 1), minutes (pos 2) and, optionally, the am/pm string (pos 3).
Each of these values is then tested against an allowed range (days: 1 - 31; months: 1 - 12; years: 1902 - present; ...).
Note: This still doesn't confirm that the date is valid. To do that you need to take into account variation between calendar months and leap-/non leap-years which is beyond the scope of this example. The following article on Form Validation: Credit Cards and Dates addresses this issue a bit better.
- Sent (0)
- Nouveau
Générez vos vidéos d'entreprise par l'IA avec la voix ou simplement du texte
chatGPTaz.com
Parlez à ChatGPT dans votre langue maternelle
AppAIVidéo
Votre première application vidéo AI GRATUITE
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Powerful AI Presentation PPT Maker for FREE
Build an impressive presentation with our free online AI presentation app
Your next top AI Assistant
Claude AI, developed by Anthropic
Your next top AI Assistant
Claude AI, developed by Anthropic
Temu gratuit 500 $ pour les nouveaux utilisateurs
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Crédits publicitaires TikTok gratuits
Maîtrisez les publicités TikTok pour le marketing de votre entreprise
Dall-E-OpenAI.com
Générez automatiquement des images créatives avec l'IA
chatGPT4.win
Parlez à ChatGPT dans votre langue maternelle
Premier produit d'intelligence artificielle d'Elon Musk - Grok/UN.com
Parlez au chatbot Grok AI dans votre langue
Outily.win
Centre d'outils ouvert et gratuit, utilisable par tous et pour tous, avec des centaines d'outils
GateIO.gomymobi.com
Airdrops gratuits à réclamer et à partager jusqu'à 150 000 $ par projet
iPhoneKer.com
Économisez jusqu'à 630 $ à l'achat d'un nouvel iPhone 16
Acheter le robot Tesla Optimus
Commandez votre robot Tesla Bot : Optimus Gen 2 dès aujourd'hui pour moins de 20 000 $
R�ponse