martes, 21 de marzo de 2017

Formulario en el que introducimosnombre producto, precio, nos suma el iva y nos da el precio final

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Validación de un formulario</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
function valida() {
    var ok = true;
    var msg = "Debes escribir algo en los campos:\n";//\n es retorno de linia
    var f=document.form1;
    if(f.txtNombre.value == ""){
        msg += "- Nombre producto\n";
        ok = false;
    }
   
    if((f.txtPrecio.value == "")||isNaN(f.txtPrecio.value)){
        msg += "- Precio\n";
        ok = false;
    }
        else{
            f.txtfinal.value=(f.txtPrecio.value)*1.16;
        }
   
   
   
    //Salida
    if(ok == false)
        alert(msg);
    //return ok;
}
</script>
</head>
<body>
<form name="form1" id="form1" action="#" method="post">   
<table border="0">
    <tr>
        <td>Nombre producto(*):</td>
        <td><input type="text" name="txtNombre" size="20"/></td>
    </tr>
    <tr>
        <td>Precio(*):</td>
        <td><input type="text" name="txtPrecio" size="20"/></td>
        <td>€</td>
    </tr>
    <tr>
        <td>IVA 16%</td>
    </tr>
    <tr>
        <td>Precio Final:</td>
        <td><input type="text" name="txtfinal" size="20" readonly="readonly"/></td>
        <td>€</td>
    </tr>
</table>
<input type="button" name="btnEnviar" value="Enviar >>" onclick="valida()"/></br>
<input type="reset" value="Borrar" />
</form>
</body>
</html>

No hay comentarios:

Publicar un comentario