tener problemas al ejecutar la verificación de edad JS

  • Zealous
  • Guru
  • Guru
  • Avatar de Usuario
  • Registrado: Abr 15, 2011
  • Mensajes: 1201
  • Loc: Sydney
  • Status: Offline

Nota Septiembre 15th, 2012, 8:50 pm

sido jugando con JS hoy y encontrado este código aunque buscando fragmentos y ácaros ser el final de un largo día pero no costura tener el código eco en la página que hice. hay algo sencillo que me falta estoy seguro pero pregunto si puedo conseguir ayuda mostrando esto.

Código: [ 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
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Septiembre 15th, 2012, 8:50 pm

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • Avatar de Usuario
  • Registrado: Dic 20, 2002
  • Mensajes: 8934
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Nota Septiembre 17th, 2012, 1:34 pm

¿Usted nunca averiguar esto? ¿Cuál es la parte HTML que la acompaña (el correspondiente parte de todas formas). Tal vez me perdí, pero ¿cómo están llamando a la función de checkAge()?
Ozzu Hosting - Want your website on a fast server like Ozzu?

Publicar Información

  • Total de mensajes en este tema: 2 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 145 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC