簡體   English   中英

表單驗證失敗但仍然提交

[英]Form validation fails but still submits

我有以下JS代碼:

function checkFull(id) {
    var input = $('#' + id).val();  
    if (input == "") {
        return false;   
    }
    else {
        return true;
    }
}

function checkSame(id1,id2) {
    var input1 = $('#' + id1).val();    
    var input2 = $('#' + id2).val();
    if (input1 == input2) {
        return true;
    }
    else {
        return false;
    }
}
function showErrors() {
    if (!checkFull('username')) {
        $('#userfield').show(); 
    }
    else {
        $('#userfield').hide(); 
    }

    if (!checkFull('email')) {
        $('#notsame').show();   
    }
    else {
        $('#notsame').hide();   
    }

    if (!checkFull('confemail')) {
        $('#notsame2').show();  
    }
    else {
        $('#notsame2').hide();  
    }

    if (!checkFull('espncode')) {
        $('#nocode').show();    
    }
    else {
        $('#nocode').hide();    
    }

    if (notSame('email','confemail')) {
        $('#notsame2').show();
        $('#notsame').show();   
    }
    else {
        $('#notsame2').hide();  
        $('#notsame').hide();
    }
}
function valform() {
    showErrors();
    if (checkFull('username') && checkFull('email') && checkFull('confemail') && checkSame('email','confemail') && checkFull('espncode')) {
        form.submit();
        alert('success');
    }
    else {

        return false;
    }
}

以下形式:

<form method="post" action="forms/post.asp" onsubmit="valform();return false">
            <strong>Poker Username</strong> <span id="userfield" style="display:none;color:#F00">*</span><br />
            <input type="text" name="username" id="username" style="width:230px;" />
            <br /><br />
            <div class="vert">
                <strong>Email Address</strong> <span id="notsame" style="display:none;color:#F00">*</span><br />
                <input type="text" name="email" id="email" style="width:230px;" />
            </div>
            <div class="vert2">
                <strong>Confirm Email Address</strong> <span id="notsame2" style="display:none;color:#F00">*</span><br />
                <input type="text" name="confemail" id="confemail" style="width:230px;" />
            </div>
            <div style="clear:both;"></div>
            <div class="espn">
                <div class="txt"><strong>Enter the ESPN special code</strong></div>
                <input type="text" name="espncode" id="espncode" style="width:230px;" />
            </div>
            <div id="nocode" style="display:none">
                You need to enter the code above
            </div>
            <div class="tc">
                <input type="checkbox" id="agree" name="agree" /> <strong>You agree to the terms and conditions of this sweepstake. Only one entry per person per sweepstake period. Any additional entries will be disqualified.</strong>
            </div>
            <br />
            <div class="submit">
                <input type="image" src="submit_BTN.png" />
            </div>
        </form>

即使我將表格留空,當我按下提交時,它也會通過。 這是為什么?

您需要從submit處理程序中的validate函數返回值:

onsubmit="valform();return false"

應該:

onsubmit="return valform();"

每當valform返回false時,這將阻止表單提交。

順便說一下,你的valform函數中的form.submit()調用是沒有意義的。 如果函數沒有返回false,它將被提交。

if (/*validity checks pass*/) {
    alert('success');
}
else {
    return false;
}

但是, 用這個小提琴查看你的代碼我注意到有一個錯誤:

ReferenceError:找不到變量:notSame

如果您修復了函數的名稱,它將起作用。 您應該使用可用的開發人員工具來調試這些小錯誤。 Firebug for Firefox和Chrome / Safari都內置了一個網絡檢查器.Opera也有Dragonfly。

可能的原因:

  • onsubmit="valform();return false"更改為onsubmit="valform();" 在形式。

嘗試,如果仍然沒有工作然后告訴。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM