Form E-mail Validation in JSP

Asked

Viewed 568 times

0

I am trying to validate a form of a JSP through Javascript but I am not able to apply the Script correctly, what I am 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>  

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 sending a form and only then "calling the check function" that doesn’t even exist! Try otherwise...

changes 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>
  • Thanks friend I will test, the "x" used in Javascript does not have to have the same name of my field E-mail in the form?

  • our... I didn’t even notice... the name of the form and the field also need to be corrected! Take a look at the HTML source code and rename the variables. It’s easier.

  • but I have a problem, the email field only has the Property="dsEmail", when I try to enter an id or a name it brings me error, do you have any idea how I can do it? I don’t know much about JSP, I’m a beginner in Java so sorry if any questions seem stupid ^^

  • I can only change the variable names in Script because the form is working correctly in the JAVA part

  • Do the following... go to the page that was generated and see the source code. All the jsp code is converted to html that your browser interprets. Uses the name that was generated by jsp. probably the Property will give the name to the input text field of the email. Look there.

  • I went to HTML, I found the names that spoke and replace where I thought it was right, but when clicking the send button nothing happens and still saving in the database any data inserted, replace correctly in Script??

  • Updates the code in the question... =)

  • All right, I updated :)

  • 1

    Almost there... that line is wrong... var dsEmail=Document.captacaoEmailsForm[dsEmail]. value; dsEmail here should be in quotes otherwise it will be the dsEmail object that is null.

  • It worked, THANK YOU VERY MUCH. and I was able to understand my mistake, this is more important! Thanks :)

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.