在表单中使用enctype =“multipart / form-data”时,JS验证无效

时间:2016-10-24 07:52:51

标签: javascript forms validation multipartform-data

我创建了一个表单,用于读取用户详细信息并具有文件上载字段。 问题是当我不使用表单的'enctype'属性时,我的JavaScript验证工作正常。但是当我使用它时,请求会在单击提交按钮时传递到Servlet。

以下是相同的JSP代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

    <script type="text/javascript" src="validateRetailer.js"></script>
    <script>

        function isNumber(evt) {
            evt = (evt) ? evt : window.event;
            var charCode = (evt.which) ? evt.which : evt.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                return false;
            }
            return true;
        }


    </script>

    <%@ include file="NewFile1.jsp" %>

</head>

<body>
<center><h1>Register New Retailer </h1>
    <form name="RegisterRetailerForm" action="RegisterRetailerController" method="post"
          onsubmit="return RetailerValidate()" enctype="multipart/form-data">

        <table>


            <tr>
                <td> Name</td>
                <td><input type="text" name="retailerName"></td>
            </tr>
            <tr>
                <td> Contact 1</td>
                <td><input type="text" name="retailerContact1" onkeypress="return isNumber(event) "></td>
            </tr>
            <tr>
                <td> Contact 2</td>
                <td><input type="text" name="retailerContact2" onkeypress="return isNumber(event)"></td>
            </tr>

            <tr>
                <td> Address Line 1</td>
                <td><input type="text" name="retailerAddress1"></td>
            </tr>
            <tr>
                <td> Address Line 2</td>
                <td><input type="text" name="retailerAddress2"></td>
            </tr>
            <tr>
                <td> PinCode</td>
                <td><input type="text" name="zip" id="zip" onkeypress="return isNumber(event)" onchange="codeAddress()">
                </td>
            </tr>
            <tr>
                <td> City</td>
                <td><input type="text" name="city" id="city" readonly="readonly">
                </td>
            </tr>
            <tr>
                <td> State</td>
                <td><input type="text" name="state" id="state" readonly="readonly">
                </td>
            </tr>

            <tr>
                <td> SetTop Box Limit</td>
                <td><input type="text" name="retailerSTBLimit" onkeypress="return isNumber(event)"></td>
            </tr>
            <tr>
                <td> Credit Limit</td>
                <td><input type="text" name="retailerCreditLimit" onkeypress="return isNumber(event)"></td>
            </tr>
            <tr>
                <td> Commission % on Sale</td>
                <td><input type="text" name="retailerCommission" onkeypress="return isNumber(event)"></td>
            </tr>
            <tr>
                <td> Service Charges</td>
                <td><input type="text" name="retailerServiceCharge" onkeypress="return isNumber(event)"></td>
            </tr>
            <tr>
                <td> Inventory List</td>
                <td><input type="file" name="fileUpload" id="upload" onchange="validate_fileupload()"></td>

            </tr>
            <tr>
                <td> Retailer Creation Date</td>
                <td><input id="creationDate" name="creationDate" type="text" value="" readonly="readonly">


                    <script>
                        var today = new Date();
                        var dd = today.getDate();
                        var mm = today.getMonth() + 1; //January is 0!
                        var yyyy = today.getFullYear();

                        if (dd < 10) {
                            dd = '0' + dd;
                        }

                        if (mm < 10) {
                            mm = '0' + mm;
                        }

                        today = mm + '/' + dd + '/' + yyyy;
                        document.RegisterRetailerForm.creationDate.value = today;
                    </script>
                </td>
            </tr>
            <tr>
                <td> Total Cost of Inventory</td>
                <td></td>
            </tr>
            <tr>

                <td colspan="2">
                    <center><input type="submit" value="Register"></center>
                </td>

            </tr>
        </table>

    </form>
</center>
</body>
</html>

以下是验证码。以防万一。 (的 validateRetailer.js

function RetailerValidate() {

    var name = document.RegisterRetailerForm.retailerName;
    var contact1 = document.RegisterRetailerForm.retailerContact1;
    var contact2 = document.RegisterRetailerForm.retailerContact2;
    var address1 = document.RegisterRetailerForm.retailerAddress1;

    var address2 = document.RegisterRetailerForm.retailerAddress2;

    var zip = document.RegisterRetailerForm.zip;

    var city = document.RegisterRetailerForm.city;

    var state = document.RegisterRetailerForm.state;
    var stblimit = document.RegisterRetailerForm.retailerSTBLimit;
    var crlimit = document.RegisterRetailerForm.retailerCreditLimit;
    var comper = document.RegisterRetailerForm.retailerCommission;
    var sc = document.RegisterRetailerForm.retailerServiceCharge;
    var il = document.RegisterRetailerForm.fileUpload;


    if (nameV(name)) {
        if (contact1V(contact1)) {

            if (contact2V(contact2)) {
                if (address1V(address1)) {
                    if (address2V(address2)) {
                        if (zipV(zip)) {
                            if (stbV(stblimit)) {
                                if (creditV(crlimit)) {
                                    if (comperV(comper)) {
                                        if (serviceV(sc)) {
                                            if (!il.value.length == 0) {

                                                return true;
                                            }
                                            else {
                                                alert('upload file');
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    return false;
}

function valid(fileName) {
    if (fileName.value.length == 0) {
        alert('Filename cannot be empty');
        return false;
    }
    return true;
}

function nameV(name) {
    if (name.value == "") {
        alert('Name cannot be blank');
        return false;
    }

    return true;
}


function contact1V(contact1) {

    if (contact1.value == "" || contact1.value.length != 10) {
        alert('Enter a valid phone number(1)');
        return false;

    }
    return true;
}


function contact2V(contact2) {

    if (contact2.value == "" || contact2.value.length != 10) {
        alert('Enter a valid phone number (2)');
        return false;

    }
    return true;
}
function address1V(address1) {

    if (address1.value == "") {
        alert('Address Line cannot be empty');
        return false;

    }
    return true;
}
function address2V(address2) {

    if (address2.value == "") {
        alert('Address Line cannot be empty');
        return false;

    }
    return true;
}

function zipV(zip) {

    if (zip.value == "" || zip.value.length != 6) {
        alert('Invalid or empty pin code field');
        return false;

    }


    return true;
}


function stbV(stblimit) {
    if (stblimit.value == "") {
        alert('SetTop Box Limit should be entered');
        return false;

    }
    return true;
}

function creditV(crlimit) {
    if (crlimit.value == "") {
        alert('Credit Limit for retailer should be entered');
        return false;

    }
    return true;
}
function comperV(comper) {
    if (comper.value == "") {
        alert('Enter commission charge in percentage');
        return false;

    }
    return true;
}

function serviceV(sc) {
    if (sc.value.length == 0) {
        alert('Enter service charge');
        return false;

    }
    return true;
}

0 个答案:

没有答案