如何在验证之后但在提交之前显示警报

时间:2014-03-03 19:10:59

标签: javascript html5

我已经找到了所有的验证代码但是我不太确定如何在提交表单之前编写警报以便弹出,但在验证完成之后。

<!DOCTYPE html>
<html>
<head>
    <title>Week 8 Lab - JavaScript DOM and Arrays</title>
    <meta charset="utf-8">
    <link href="css/style.css" rel="stylesheet">
</head>
<body>
<script>
    function validate()
    {
        var fName = document.forms["orderForm"].firstName.value;//first name validation
        if(fName==null || fName=="")
        {
            document.getElementById('firstNameError').innerHTML= "Please enter a first name.";
            return false;
        }


        var lName = document.forms["orderForm"].lastName.value;//last name validation
        if(lName==null || lName=="")
        {
            document.getElementById('lastNameError').innerHTML= "Please enter a last name.";
            return false;
        }


        var address = document.forms["orderForm"].address.value;//address validation
        if(address==null || address=="")
        {
            document.getElementById('addressError').innerHTML= "Please enter an address.";
            return false;
        }


        var city = document.forms["orderForm"].city.value;//city validation
        if(city==null || city=="")
        {
            document.getElementById('cityError').innerHTML= "Please enter a city.";
            return false;
        }


        var pCodeCheck = /^[0-9a-zA-Z]+$/;//postal code validation
        if(postalCode.value.match(pCodeCheck))
        {
            //do nothing
            return true;
        }
        else
        {
            document.getElementById('postalCoderror').innerHTML= "Please enter a valid postal code.";
            return false;
        }

                    // makes sure you cannot order a negative number of items
        var itemQTY = document.forms["orderForm"].widget1qty.value;
        if(itemQTY < 0)
        {
            document.getElementById('qtyError').innerHTML= "You cannot enter a negative number.";
            return false;
        }


        var itemQTY2 = document.forms["orderForm"].widget2qty.value;
        if(itemQTY2 < 0)
        {
            document.getElementById('qtyError2').innerHTML= "You cannot enter a negative number.";
            return false;
        }


        var itemQTY3 = document.forms["orderForm"].widget3qty.value;
        if(itemQTY3 < 0)
        {
            document.getElementById('qtyError3').innerHTML= "You cannot enter a negative number.";
            return false;
        }

                    //makes sure there is at least one item ordered
        var wid1Qty = document.getElementById('widget1qty').value;
        var wid2Qty = document.getElementById('widget2qty').value;
        var wid3Qty = document.getElementById('widget3qty').value;
        if(wid1Qty + wid2Qty + wid3Qty == 0)
        {
            document.getElementById('itemQTY').innerHTML= "You must order atleast one item.";
            return false;
        }

    /*
                                // trying to send alert with the order total.
                                // not sure how to call it after the validation is done.
            var total1;         
            var total2;
            var total3:
            var total4;

            if(document.getElementById('widget1qty').value == 0)
            {
                total1 = 0;
            }

            else(document.getElementById('widget1qty').value != 0)
            {
                total1 = document.getElementById('widget1qty').value * 5;
            }


            if(document.getElementById('widget2qty').value == 0)
            {
                total2 = 0;
            }

            else(document.getElementById('widget2qty').value != 0)
            {
                total2 = document.getElementById('widget2qty').value * 15;
            }


            if(document.getElementById('widget3qty').value == 0)
            {
                total3 = 0;
            }

            else(document.getElementById('widget3qty').value != 0)
            {
                total3 = document.getElementById('widget3qty').value * 25;
            }


            total4 = (total1 + total2 + total3)
            alert('Your total is: $' + total4 + '.00');
            */
    }
</script>
    <div id="wrapper">
    <h2 class="center">Order Form</h2> <!-- action="processForm.html"       "javascript:void(0)" -->
    <form name="orderForm" method="post" onsubmit="validate();" action="processForm.html">
    <fieldset>
    <legend>Personal Information</legend>
    <table>

        <tr>
            <th colspan="3"></th>
        </tr>
        <tr>
            <td><span class="required">*</span>First Name:</td>
            <td><input type="text" name="firstName" id="firstName" size="30"></td>
            <td id="firstNameError"></td>
        </tr>
        <tr>
            <td><span class="required">*</span>Last Name:</td>
            <td><input type="text" name="lastName" id="lastName" size="30"></td>
            <td id="lastNameError"></td>
        </tr>
        <tr>
            <td><span class="required">*</span>Address:</td>
            <td><input type="text" name="address" id="address" size="30"></td>
            <td id="addressError"></td>
        </tr>
        <tr>
            <td><span class="required">*</span>City:</td>
            <td><input type="text" name="city" id="city" size="30"></td>
            <td id="cityError"></td>
        </tr>
        <tr>
            <td><span class="required">*</span>Province:</td>
            <td><select name="province" id="province" size="1">
                    <option disabled>Select a province</option>
                    <option value="BC">British Columbia</option>
                    <option value="AB">Alberta</option>
                    <option value="SK">Saskatchewan</option>
                    <option value="MB">Manitoba</option>
                    <option value="ON">Ontario</option>
                    <option value="QC">Québec</option>
                    <option value="NB">New Brunswick</option>
                    <option value="NS">Nova Scotia</option>
                    <option value="PE">Prince Edward Island</option>
                    <option value="NF">Newfoundland</option>
                    <option value="YK">Yukon</option>
                    <option value="NWT">Northwest Territories</option>
                    <option value="NU">Nunavut</option>
                </select>
            </td>
            <td></td>
        </tr>
        <tr>
            <td><span class="required">*</span>Postal Code:</td>
            <td><input type="text" name="postalCode" id="postalCode" maxlength="6"></td>
            <td id="postalCoderror"></td>
        </tr>
        </table>
        </fieldset>
        <fieldset>
        <legend>Order Information</legend>

        <table>
        <tr>
            <th colspan="3"></th>
        </tr>
        <tr>
            <td rowspan="3">Select your products:<br>
            <td>Widget #1&nbsp;
                <input type="text" name="widget1qty" id="widget1qty" size="1" value="0">Qty @ <strong>$5.00/ea</strong></td>
                <td id="qtyError"></td>
        </tr>
        <tr>
            <td>Widget #2&nbsp;
                <input type="text" name="widget2qty" id="widget2qty" size="1" value="0">Qty @ <strong>$15.00/ea</strong></td>
                <td id="qtyError2"></td>
        </tr>
        <tr>
            <td>Widget #3&nbsp;
                <input type="text" name="widget3qty" id="widget3qty" size="1" value="0">Qty @ <strong>$25.00/ea</strong></td>
                <td id="qtyError3"></td>
        </tr>
        <tr>
        <td rowspan="3"></td>
        <td></td>
        <td id="itemQTY"></td>
        </tr>
        <tr></tr><tr></tr><tr></tr>
        <tr>
            <td rowspan="3">Shipping Type:</td>
            <td>Standard ($5.00)<input type="radio" name="shippingType" id="shippingTypeStandard" value="Standard" checked></td>

        </tr>
        <tr>
            <td>Express ($10.00)<input type="radio" name="shippingType" id="shippingTypeExpress" value="Express"></td>
        </tr>
        <tr>
            <td>Overnight ($20.00)<input type="radio" name="shippingType" id="shippingTypeOvernight" value="Overnight"></td>
        </tr>

        </table>
        </fieldset>
        <fieldset>
        <legend>Submit Order</legend>
        <table>
        <tr>
            <th colspan="2"></th>
        </tr>
        <tr>
            <td><input type="submit" name="btnSubmit" id="btnSubmit" onclick="return validate();" value="Submit Order"></td>
            <td><input type="reset" name="btnReset" id="btnReset" value="Reset Form"></td>
        </tr>
    </table>
    </fieldset>
    </form>
</div>  
</body>

我只是不知道在验证完成后如何编码它以便弹出。

3 个答案:

答案 0 :(得分:0)

在您的表单中,您可以放置​​id = "myForm",然后在javascript中执行此操作:

    function validateForm() {
var fName = document.forms["orderForm"].firstName.value;//first name validation
        if(fName==null || fName=="")
        {
            document.getElementById('firstNameError').innerHTML= "Please enter a first name.";
            return false;
        }


        var lName = document.forms["orderForm"].lastName.value;//last name validation
        if(lName==null || lName=="")
        {
            document.getElementById('lastNameError').innerHTML= "Please enter a last name.";
            return false;
        }


        var address = document.forms["orderForm"].address.value;//address validation
        if(address==null || address=="")
        {
            document.getElementById('addressError').innerHTML= "Please enter an address.";
            return false;
        }


        var city = document.forms["orderForm"].city.value;//city validation
        if(city==null || city=="")
        {
            document.getElementById('cityError').innerHTML= "Please enter a city.";
            return false;
        }


        var pCodeCheck = /^[0-9a-zA-Z]+$/;//postal code validation
        if(postalCode.value.match(pCodeCheck))
        {
            //do nothing
            return true;
        }
        else
        {
            document.getElementById('postalCoderror').innerHTML= "Please enter a valid postal code.";
            return false;
        }

                    // makes sure you cannot order a negative number of items
        var itemQTY = document.forms["orderForm"].widget1qty.value;
        if(itemQTY < 0)
        {
            document.getElementById('qtyError').innerHTML= "You cannot enter a negative number.";
            return false;
        }


        var itemQTY2 = document.forms["orderForm"].widget2qty.value;
        if(itemQTY2 < 0)
        {
            document.getElementById('qtyError2').innerHTML= "You cannot enter a negative number.";
            return false;
        }


        var itemQTY3 = document.forms["orderForm"].widget3qty.value;
        if(itemQTY3 < 0)
        {
            document.getElementById('qtyError3').innerHTML= "You cannot enter a negative number.";
            return false;
        }

                    //makes sure there is at least one item ordered
        var wid1Qty = document.getElementById('widget1qty').value;
        var wid2Qty = document.getElementById('widget2qty').value;
        var wid3Qty = document.getElementById('widget3qty').value;
        if(wid1Qty + wid2Qty + wid3Qty == 0)
        {
            document.getElementById('itemQTY').innerHTML= "You must order atleast one item.";
            return false;
        }
        var total1;
        var total2;
        var total3;
        var total4;

        total1 = document.forms['orderForm']['widget1qty'].value * 5;
        total2 = document.forms['orderForm']['widget2qty'].value * 15;
        total3 = document.forms['orderForm']['widget3qty'].value * 25;
        total4 = (total1 + total2 + total3)
        alert('Your total is: $' + total4 + '.00');
        return;
    }
function startValidate(){
        validateForm();
        document.forms['orderForm'].submit();
}

编辑: 只需在validateForm()函数中添加所有其他信息即可。 我没有看到它,因为我在添加HTML和其他JS之前开始编辑。

根据War10ck的帖子,你应该改变这个:

<input type="submit" name="btnSubmit" id="btnSubmit" onsubmit="startValidate()" value="Submit Order">

答案 1 :(得分:0)

您可以创建另一个名为FireOnSubmit的函数,并执行以下操作:

`

function FireOnSubmit(){
   if(validate()==true) alert(""form validated..is being redirected")
   else { 
          alert("invalid form data");
          return false;
   }

}

`

您可以将FireOnSubmit放在表单的onsubmit而不是validate函数上。

此外,您需要从验证功能返回true或false。

答案 2 :(得分:0)

而不是所有的JavaScript,这应该也适用 在您选择的所有字段都已填写之前,无法单击提交按钮 然后会弹出一条警告信息。

这进入了头部

<script>

function checkform()
{
var f = document.forms["testform"].elements;
var cansubmit=true;
for (var i = 0; i < f.length; i++) {
if (f[i].value.length==0) cansubmit=false;
}
if (cansubmit)
{
document
.getElementById('submit'
).disabled=false;
}
}
</script>

这是表格。提交按钮被禁用,直到填满所有字段。

<form name="testform">
<input type="text" onKeyup="checkform()" placeholder="First Name"required/><br>
<input type="text" onKeyup="checkform()" placeholder="Last Name" required/><br>
<input id="submit" type="submit" disabled="disabled" value="Submit" onclick="alert('Your Alert Here...')"/>
</form>