Validation of E-mail in form in the JSP

0

I'm trying to validate a form from a JSP via JavaScript but I'm not able to apply the Script correctly, what am I doing wrong?

<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<SCRIPT>
//teste de script
window.alert("teste, funcionando!")
/*função valida email*/
function verificarEEnviar()
{
    var dsEmail=document.captacaoEmailsForm[dsEmail].value;
    var atpos=dsEmail.indexOf("@");
    var dotpos=dsEmail.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=dsEmail.length)
        {
            alert("Não é um endereço de e-mail válido");
            return false;
        }
    return document.captacaoEmailsForm.submit();

}
</SCRIPT>

<div class="captacao">
    <html:form action="/captacaoEmails" styleId="cadastroCliente2"> 
    <div class="captacao-titulo">Cadastre-se e receba<span> ofertas exclusivas </span> </div>
    <div class="captacao-email"> 
        <html:text tabindex="5" property="dsEmail" maxlength="60"/><html:errors property="dsEmail" header="empty"/>
        <a href="javascript:verificarEEnviar();" id="btContinuar" class="captacao fr">Enviar</a>
    </div>
    </html:form>


</div>  
    
asked by anonymous 29.09.2014 / 20:32

1 answer

1

The problem is the link:

<a href="javascript:document.captacaoEmailsForm.submit(); javascript:verificar" id="btContinuar" class="captacao fr">Enviar</a>

You are submitting a form and only after "calling the check function" it does not even exist! Try the opposite ...

change the link to

javascript:verificarEEnviar()

And the script ...

<SCRIPT>
//teste de script
window.alert("teste, funcionando!")
/*função valida email*/
function verificarEEnviar()
{
    var x=document.forms["myForm"]["email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
        {
            alert("Não é um endereço de e-mail válido");
            return false;
        }
    return document.forms["myForm"].submit();
}
</SCRIPT>
    
29.09.2014 / 20:48