Jump to content
Fórum Script Brasil
  • 0

Não apagar oque já escrevi!


bcs_ptx

Question

Eai pessoal, esse form que fiz se o usuário preencher tudo e esquecer um item obrigatório,

logo após ser chamada a função alert(), ele apaga tudo oque já escrevi, como fazer para ele avisar oque não for preenchido e não apagar oque já escrevi?

Abraços...

<script type="text/javascript">

function calcula(){

        var kmInicial = parseFloat(document.form.km_inicial.value);     
        var kmFinal = parseFloat(document.form.km_final.value);
        var valorCombustivel = parseFloat(document.form.valor_combustivel.value);
        
        if (isNaN(kmInicial)){
                
                kmInicial = 0;
                
        }
        
        if (isNaN(kmFinal)){
        
                kmFinal = 0;
                
        }
        
        if (isNaN(valorCombustivel)){
        
                valorCombustivel = 0;
                
        }
        
        var total = kmFinal - kmInicial;
        
        var valorTotal = (total * valorCombustivel) / 10;
        
        document.getElementById("valorTotal").value = 'R$ ' +  valorTotal;
        
}

function valida(){
        
        if(document.form.km_inicial.value > document.form.km_final.value){
        
                alert('KM INICIAL não pode ser maior que KM FINAL!!!');
        
        }
        
        if(document.form.valor_combustivel.value == ''){
        
                alert('Campo VALOR COMBUSTÍVEL em branco!!!');
        
        }
        
        if(document.form.km_inicial.value == ''){
        
                alert('Campo KM INICIAL em branco!!!');
        
        }
        
        if(document.form.km_final.value == ''){
        
                alert('Campo KM FINAL em branco!!!');
        
        }
        
        else
        
        {
        
                form.submit();
        
        }

}

</script>
    
<form name="form" method="post">

    VALOR COMBUSTIVEL<input type="text" name="valor_combustivel" value="" onchange="calcula();" />
    
    <br>
    
    KM INICIAL<input type="text" name="km_inicial" value="" onchange="calcula();" />
    
    <br>
    
    KM FINAL<input type="text" name="km_final" value="" onchange="calcula();" />
    
    <br>
    
    TOTAL <input type="text" id="valorTotal" name="valorTotal" value="" readonly="readonly" />
    
    <br />
    
    <input type="submit" value="Salvar" onclick="valida(this.form);"/>
    
</form>

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...