Jquery Value match Regex [duplicar]
Esta pregunta ya tiene una respuesta aquí:
Estoy tratando de validar la entrada de correo electrónico a través de jQuery:
Mi jQuery
<script>
/* <![CDATA[ */
jQuery(function(){
$( ".mail" ).keyup(function() {
var VAL = $(this).val();
var email = new RegExp(^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$);
if(VAL.test(email)){
alert('Great, you entered an E-Mail-address');
}
});
});
/* ]]> */
</script>
Esto no alertará a pesar de que entré [email protected]. Ya lo intenté .test () y .match (), ¿qué hice mal?
31
2 answers
- Pasa una cadena a RegExp o crea una regex usando la sintaxis
//
- Llama
regex.test(string)
, nostring.test(regex)
So
jQuery(function () {
$(".mail").keyup(function () {
var VAL = this.value;
var email = new RegExp('^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$');
if (email.test(VAL)) {
alert('Great, you entered an E-Mail-address');
}
});
});
55
Author: Arun P Johny,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-29 08:09:15
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-29 08:09:15
Cámbialo a esto:
var email = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
Este es un literal de expresión regular al que se le pasa la bandera i
que significa ser insensible a mayúsculas y minúsculas.
Tenga en cuenta que la validación de direcciones de correo electrónico es dura (hay una expresión regular de 4 o 5 páginas al final de Dominar las Expresiones Regulares que lo demuestran) y su expresión ciertamente no capturará todas las direcciones de correo electrónico válidas.
10
Author: Sean Bright,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-12 12:06:49
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-12 12:06:49