Skip to content

Instantly share code, notes, and snippets.

@danilowm
Created March 8, 2012 15:18
Show Gist options
  • Save danilowm/2001467 to your computer and use it in GitHub Desktop.
Save danilowm/2001467 to your computer and use it in GitHub Desktop.
Validar email com Javascript
<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>
@Zenildo-Araujo
Copy link

Felizmente consegui encontrar um script que funciona.

@kahbeltcher
Copy link

Me ajudou muito a resolver o meu vlw

@thaynalp
Copy link

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