Condition and Validation of dates

0

I have 3 fields: contract number, start date and end date.

How do I validate two fields simultaneously? That is, the start date and end date must be fulfilled. If they are not, I have to send only ONE message (not two, as I am used to - one for each field).

To submit this form, it is enough that either the contract number or the dates are fulfilled. There is no need to fill in both.

    
asked by anonymous 02.10.2014 / 05:02

1 answer

0

I think you solve this way with cascading ifs:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
function emitir(){
    var numero = document.forms["formEmitir"].numero.value;
    var dataini = document.forms["formEmitir"].dataini.value;
    var datafim = document.forms["formEmitir"].datafim.value;
    if(!numero && !dataini && !datafim){
        alert("Preencha com o número do contrato ou as datas!");
    } else {
        if(numero && (dataini || datafim)){
            alert("Preencha apenas o número OU as datas!");
        } else if(numero) {
            alert("Será emitido através do número do contrato...");
        } else if(dataini && datafim){
            alert("Será emitido através das datas...");
        } else {
            alert("Preencha ambas as datas de início e fim!");
        }
    }
}
</script>
</head>

<body>
<form name="formEmitir">
<fieldset>
    <legend>Seu Contrato</legend>
    <p>Pelo número: <input type="text" name="numero" /></p>
    <p>Pela data: de <input type="text" name="dataini" /> a <input type="text" name="datafim" /></p>
    <p><button onclick="emitir();">Emitir</button></p>
</fieldset>
</form>
</body>
</html>
    
02.10.2014 / 14:15