Foro de elhacker.net

Programación => Desarrollo Web => Mensaje iniciado por: Leguim en 23 Septiembre 2019, 03:37 am



Título: [Pregunta]: Validar email con javascript
Publicado por: Leguim en 23 Septiembre 2019, 03:37 am
Buenas noches,
quería saber como podría validar un correo electrónico usando javascript, para validar emails hago usando PHP con el siguiente código:

Código
  1. function Validate_Email($email)
  2. {
  3. $result = false;
  4.  
  5. if(is_string($email))
  6. {
  7. if(filter_var($email, FILTER_VALIDATE_EMAIL))
  8. {
  9. $result = true;
  10. }
  11. }
  12.  
  13. return $result;
  14. }
  15.  

La idea sería hacerlo lo más similar posible a como lo hago con PHP pero bueno tampoco quiero complicarlos...


Título: Re: [Pregunta]: Validar email con javascript
Publicado por: @XSStringManolo en 23 Septiembre 2019, 03:55 am
FILTER_VALIDATE_EMAIL no es magia negra. Tiene un código detrás.

Código
  1. void php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
  2. {
  3. /*
  4. * The regex below is based on a regex by Michael Rushton.
  5. * However, it is not identical.  I changed it to only consider routeable
  6. * addresses as valid.  Michael's regex considers a@b a valid address
  7. * which conflicts with section 2.3.5 of RFC 5321 which states that:
  8. *
  9. *   Only resolvable, fully-qualified domain names (FQDNs) are permitted
  10. *   when domain names are used in SMTP.  In other words, names that can
  11. *   be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed
  12. *   in Section 5) are permitted, as are CNAME RRs whose targets can be
  13. *   resolved, in turn, to MX or address RRs.  Local nicknames or
  14. *   unqualified names MUST NOT be used.
  15. *
  16. * This regex does not handle comments and folding whitespace.  While
  17. * this is technically valid in an email address, these parts aren't
  18. * actually part of the address itself.
  19. *
  20. * Michael's regex carries this copyright:
  21. *
  22. * Copyright © Michael Rushton 2009-10
  23. * http://squiloople.com/
  24. * Feel free to use and redistribute this code. But please keep this copyright notice.
  25. *
  26. */
  27. pcre2_code *re = NULL;
  28. pcre2_match_data *match_data = NULL;
  29. uint32_t capture_count;
  30. zend_string *sregexp;
  31. int rc;
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[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iDu";
  33. const char regexp1[] = "/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD";
  34. const char *regexp;
  35. size_t regexp_len;
  36.  
  37. if (flags & FILTER_FLAG_EMAIL_UNICODE) {
  38. regexp = regexp0;
  39. regexp_len = sizeof(regexp0) - 1;
  40. } else {
  41. regexp = regexp1;
  42. regexp_len = sizeof(regexp1) - 1;
  43. }
  44.  
  45. /* The maximum length of an e-mail address is 320 octets, per RFC 2821. */
  46. if (Z_STRLEN_P(value) > 320) {
  47. RETURN_VALIDATION_FAILED
  48. }
  49.  
  50. sregexp = zend_string_init(regexp, regexp_len, 0);
  51. re = pcre_get_compiled_regex(sregexp, &capture_count);
  52. zend_string_release_ex(sregexp, 0);
  53. if (!re) {
  54. RETURN_VALIDATION_FAILED
  55. }
  56. match_data = php_pcre_create_match_data(capture_count, re);
  57. if (!match_data) {
  58. RETURN_VALIDATION_FAILED
  59. }
  60. rc = pcre2_match(re, (PCRE2_SPTR)Z_STRVAL_P(value), Z_STRLEN_P(value), 0, 0, match_data, php_pcre_mctx());
  61. php_pcre_free_match_data(match_data);
  62.  
  63. /* 0 means that the vector is too small to hold all the captured substring offsets */
  64. if (rc < 0) {
  65. RETURN_VALIDATION_FAILED
  66. }
  67.  
  68. }
  69. /* }}} */
Pórtalo a javascript. Si quieres que te quede más limpio el código para trabajar crea una carpetas libs y añades el código con Etiqueta script src ="libs/filtros/f_email.js">finEtiquetaScript


Título: Re: [Pregunta]: Validar email con javascript
Publicado por: #!drvy en 24 Septiembre 2019, 11:34 am
No tiene mucho sentido utilizar la de PHP, demasiado complicado y de hecho creo que los flags ni siquiera son compatibles. Puedes usar el estándar oficial (RFC 2822) y a correr.

Código
  1. function validateEmail(email) {
  2.    var re = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
  3.    return re.test(String(email).toLowerCase());
  4. }


https://stackoverflow.com/a/1373724/1974385

Saludos


Título: Re: [Pregunta]: Validar email con javascript
Publicado por: Leguim en 24 Septiembre 2019, 18:36 pm
No tiene mucho sentido utilizar la de PHP, demasiado complicado y de hecho creo que los flags ni siquiera son compatibles. Puedes usar el estándar oficial (RFC 2822) y a correr.

Código
  1. function validateEmail(email) {
  2.    var re = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
  3.    return re.test(String(email).toLowerCase());
  4. }


https://stackoverflow.com/a/1373724/1974385

Saludos

Hola! ¿Es decir la función PHP que uso para validar los correos no es segura? ¿o para usarlo en javascript no es recomendable?


Título: Re: [Pregunta]: Validar email con javascript
Publicado por: #!drvy en 29 Septiembre 2019, 03:37 am
Simplemente es un overkill, es segura, pero implementarla tal cual esta implementada en PHP seria invertir demasiado tiempo para algo que ya esta hecho de otras formas.

Saludos