Created
March 8, 2012 15:18
-
-
Save danilowm/2001467 to your computer and use it in GitHub Desktop.
Validar email com Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
<script type="text/javascript"> | |
$(function(){ | |
$('#form_contato').submit(function(){ | |
var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/); | |
var nome = $('#txt_nome').val(); | |
var email = $('#txt_email').val(); | |
if( nome == '' ) { alert('Preencha o campo nome'); return false; } | |
if( email == '' || !er.test(email) ) { alert('Preencha o campo email corretamente'); return false; } | |
// Se passou por essas validações exibe um alert | |
alert( 'formulário enviado com sucesso!' ); | |
}); | |
}); | |
</script> | |
<form action="" id="form_contato"> | |
<label for="txt_nome">Nome</label> <input type="text" id="txt_nome" required/> <br/> | |
<label for="txt_email">Email</label> <input type="email" id="txt_email" required/> <br/> | |
<input type="submit" value="Enviar"/> | |
</form> |
Me ajudou muito a resolver o meu vlw
obgda! Era isso que eu estava procurando!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Felizmente consegui encontrar um script que funciona.