function that clears the VALUE of the field in JSP

Asked

Viewed 168 times

0

I need to insert in my JSP a function that cleans the input value when the user clicks to enter the value, but I cannot enter class in the input, nor is Placeholder and nor Onclick accepted, could someone tell me some way to do?

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

<SCRIPT>
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");   
        }
        else{
            return document.captacaoEmailsForm.submit();
        }
        document.captacaoEmailsForm.reset();       
}
</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" onClick="this.form.reset()" value="inserir seu email" />
        <a href="javascript:verificarEEnviar();" id="btContinuar" class="captacao fr">Enviar</a>
        <br>
        <html:errors property="dsEmail" header="empty"/>
    </div>
    </html:form>
</div> 
  • It has a calling function reset could not use it? As soon as it clicks , its action inserts and gives the command in the input.

  • I can use the reset, but later I have to use an Onclick to activate the function in the field, no?

1 answer

1

I created the function 'clearThis' and called in the field where I wanted through 'onfocus="clearThis(this)' and it worked, I hope it helps those who need it as well as helped me when the code worked.

<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
  <%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    <SCRIPT>
    document.dsEmail.reset();
    /*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");
            }
            else{ return document.captacaoEmailsForm.submit(); }
        }
        function clearThis(target){
        target.value= "";
        }
    </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" value="Digite seu E-mail" onfocus="clearThis(this)"/>
            <a href="javascript:verificarEEnviar();" id="btContinuar" class="captacao fr">Enviar</a>
            <br>
            <html:errors property="dsEmail" header="empty"/>
        </div>
        </html:form>
   </div> 

Browser other questions tagged

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