ayant des problèmes de vérification de l'âge JS

  • Zealous
  • Guru
  • Guru
  • Avatar de l’utilisateur
  • Inscription: Avr 15, 2011
  • Messages: 1195
  • Loc: Sydney
  • Status: Offline

Message Septembre 15th, 2012, 8:50 pm

été jouer avec JS aujourd'hui et trouve ce code si vous cherchez les extraits et les acariens la fin d'une longue journée, mais ne peut pas couture d'avoir le code écho sur la page j'ai fait. Il y a quelque chose de simple me manque je ne sais pas mais demandais si je peux obtenir de l'aide montrant cela.

Code: [ Select ]
function checkAge()
{

var today = new Date();
var d = document.getElementById("dob").value;
if (!/\d{4}\-\d{2}\-\d{2}/.test(d))
{ // check valid format
showMessage();
return false;
}

d = d.split("-");
var byr = parseInt(d[0]);
var nowyear = today.getFullYear();
if (byr >= nowyear || byr < 1900)
{ // check valid year
showMessage();
return false;
}
var bmth = parseInt(d[1],10)-1; // radix 10!
if (bmth <0 || bmth >11)
{ // check valid month 0-11
showMessage();
return false;
}
var bdy = parseInt(d[2],10); // radix 10!
var dim = daysInMonth(bmth+1,byr);
if (bdy <1 || bdy > dim)
{ // check valid date according to month
showMessage();
return false;
}

var age = nowyear - byr;
var nowmonth = today.getMonth();
var nowday = today.getDate();
var age_month = nowmonth - bmth;
var age_day = nowday - bdy;
if (age < 18 )
{
alert ("We're sorry but thesite.com won't allow children under 18 years old to login.");
}
else if (age == 18 && age_month <= 0 && age_day <0)
{
alert ("We're sorry but thesite.com won't allow children under 18 years old to login.");
}


}

function showMessage()
{
if (document.getElementById("dob").value != "")
{
alert ("Invalid date format or impossible year/month/day of birth - please re-enter as YYYY-MM-DD");
document.getElementById("dob").value = "";
document.getElementById("dob").focus();
}
}

function daysInMonth(month,year) { // months are 1-12
var dd = new Date(year, month, 0);
return dd.getDate();
}
  1. function checkAge()
  2. {
  3. var today = new Date();
  4. var d = document.getElementById("dob").value;
  5. if (!/\d{4}\-\d{2}\-\d{2}/.test(d))
  6. { // check valid format
  7. showMessage();
  8. return false;
  9. }
  10. d = d.split("-");
  11. var byr = parseInt(d[0]);
  12. var nowyear = today.getFullYear();
  13. if (byr >= nowyear || byr < 1900)
  14. { // check valid year
  15. showMessage();
  16. return false;
  17. }
  18. var bmth = parseInt(d[1],10)-1; // radix 10!
  19. if (bmth <0 || bmth >11)
  20. { // check valid month 0-11
  21. showMessage();
  22. return false;
  23. }
  24. var bdy = parseInt(d[2],10); // radix 10!
  25. var dim = daysInMonth(bmth+1,byr);
  26. if (bdy <1 || bdy > dim)
  27. { // check valid date according to month
  28. showMessage();
  29. return false;
  30. }
  31. var age = nowyear - byr;
  32. var nowmonth = today.getMonth();
  33. var nowday = today.getDate();
  34. var age_month = nowmonth - bmth;
  35. var age_day = nowday - bdy;
  36. if (age < 18 )
  37. {
  38. alert ("We're sorry but thesite.com won't allow children under 18 years old to login.");
  39. }
  40. else if (age == 18 && age_month <= 0 && age_day <0)
  41. {
  42. alert ("We're sorry but thesite.com won't allow children under 18 years old to login.");
  43. }
  44. }
  45. function showMessage()
  46. {
  47. if (document.getElementById("dob").value != "")
  48. {
  49. alert ("Invalid date format or impossible year/month/day of birth - please re-enter as YYYY-MM-DD");
  50. document.getElementById("dob").value = "";
  51. document.getElementById("dob").focus();
  52. }
  53. }
  54. function daysInMonth(month,year) { // months are 1-12
  55. var dd = new Date(year, month, 0);
  56. return dd.getDate();
  57. }
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Septembre 15th, 2012, 8:50 pm

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • Avatar de l’utilisateur
  • Inscription: Déc 20, 2002
  • Messages: 8922
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Message Septembre 17th, 2012, 1:34 pm

A vous jamais comprendre cela ? Quelle est la partie HTML qui va avec (la partie de toute façon). Peut-être que je l'ai manqué, mais comment vous appelez la fonction checkAge() ?
Ozzu Hosting - Want your website on a fast server like Ozzu?

Afficher de l'information

  • Total des messages de ce sujet: 2 messages
  • Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 255 invités
  • Vous ne pouvez pas poster de nouveaux sujets
  • Vous ne pouvez pas répondre aux sujets
  • Vous ne pouvez pas éditer vos messages
  • Vous ne pouvez pas supprimer vos messages
  • Vous ne pouvez pas joindre des fichiers
 
 

© 2011 Unmelted, LLC. Ozzu® est une marque déposée de Unmelted, LLC