javascript 有效,但联系表仍会发送邮件

javascript works but contact form still sends mail

你好,抱歉我的英语不好

我有一个联系页面,我正在使用 javascript 创建一个 "captcha"。所以人们必须做一个数学,如果那是正确的,他们可以给我发电子邮件。现在我的问题是 javasctipt 发出警告,指出数学错误或未填写,但电子邮件仍然被播种。

我的 html:

    <script type="text/javascript">
function validate () {
    var ta = document.getElementById("ta").value;
    var answer = document.getElementById("answer").value;
    var digit1 = parseInt(document.getElementById("digit1").innerHTML);
    var digit2 = parseInt(document.getElementById("digit2").innerHTML);
    var sum = digit1+ digit2;
    if (answer === null || answer === "") {
    alert("reken de cijfers uit");
    return false;
    } else if (answer != sum){
    alert("je som is fout");
    } else if (ta === null || ta === ""){
    alert("schrijf een bericht");
    } else {
    document.getElementById("answer").innerHTML = "bezig met sturen";
    document.getElementById("answer").innerHTML = "";
    }
}

function randNums () {
    var rand_num1 = Math.floor(Math.random()*10)+1;
    var rand_num2 = Math.floor(Math.random()*10)+1;
    document.getElementById("digit1") .innerHTML=rand_num1;
    document.getElementById("digit2") .innerHTML=rand_num2;
}

</script>

<body onload="randNums() ;">

<form action="/cgi-bin/form.cgi" method="POST" onsubmit="return validate ();">
<input type="hidden" name="DEBUG" value="0">
<input type="hidden" name="MAILFILE" value="peymankarimi/form/sjabloon.txt">
<input type="hidden" name="MAILTO" value="peyman_50@hotmail.com">
<input type="hidden" name="REPLYFAULT" value="peymankarimi/form/formulier.html">
<input type="hidden" name="REPLYOK" value="peymankarimi/form/verzonden.html">

<table border="0" width="374" id="contactformulier">
<tr>
    <td width="137">Naam:</td>
    <td width="230"><input type="text"  size="31" name="naam" placeholder="Naam"></td>
</tr>
<tr>
    <td width="137">Voornaam:</td>
    <td width="230"><input type="text" size="31" name="voornaam" placeholder="Voornaam"></td>
</tr>

<tr>
    <td width="137">Woonplaats:</td>
    <td width="230"><input type="text" size="31" name="woonplaats" placeholder="Woonplaats"></td>
</tr>
<tr>
    <td width="137">Telefoonnummer:</td>
    <td width="230"><input type="text" size="31" name="telefoon" placeholder="Telefoonnummer">    </td>
</tr>
<tr>
    <td width="137">E-mailadres: <br></br></td>
    <td width="230">
    <input type="text" size="31" name="MAILFROM" placeholder="E-mailadres"> <br></br> </td>
</tr>
</tr>   
<tr>
    <td width="137">Onderwerp:</td>
    <td width="230"><input type="text" size="31" maxlength="30" name="SUBJECT"> </td>
</tr>

<tr>
    <td colspan="2">Uw vragen, opmerkingen, suggesties, ... :<br>
    <textarea id="ta" name="omschrijving" rows="6" cols="43" ></textarea>
    <br />
    <strong> Tel deze cijfers op </strong>
<span id="digit1"> </span> +
<span id="digit2"> </span> =
<input type="text" id="answer" size="2"; />
<br /> 
<p align="right"><input type="submit" name="cmdVerzenden" value="Verzenden">&nbsp;
    <input type="reset" name="cmdWissen" value="Wissen"></td>
</form>

</tr>
</table>
</form>
</body>

您的验证函数需要 return 所有错误都为 false 才能阻止表单提交。你似乎只有一个错误。

function validate () {
    var ta = document.getElementById("ta").value;
    var answer = document.getElementById("answer").value;
    var digit1 = parseInt(document.getElementById("digit1").innerHTML);
    var digit2 = parseInt(document.getElementById("digit2").innerHTML);
    var sum = digit1+ digit2;
    if (answer === null || answer === "") {
    alert("reken de cijfers uit");
    return false;
    } else if (answer != sum){
    alert("je som is fout");
    return false;
    } else if (ta === null || ta === ""){
    alert("schrijf een bericht");
    return false;
    } else {
    document.getElementById("answer").innerHTML = "bezig met sturen";
    document.getElementById("answer").innerHTML = "";
    }
}