且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

验证jsp页面文本框

更新时间:2023-12-04 18:03:10

&lt ; / th >
< th > 数量&lt ; / th >
< th > < / th >
< th > < / th >
< c:foreach var = product items =


{inventoryBean.getProductList()} xmlns:c = #unknown >
< 表格 操作 = 库存 方法 = POST >
< tr >
< td > < input type = text name = upc value =


{product.getUpc()} size = 10 / > < / td >
< td > < input type = text name = longDetails value =

I have created a jsp page that I need help validating. There are 5 textboxes on the page. 2 are text(longDetails, shortDetails) and 3 are integers(UPC,Price,Quantity). They all need to be validated to make sure the form isn't submitted with invalid data.
I have the page code here along with my script please let me know what I'm doing wrong.

<%@page import="java.util.List"%>
<%@page import="jonej102.Product"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Inventory</title>
    </head>
</script>
    <body>
        <h1>View Inventory</h1>
       <jsp:usebean id="inventoryBean" class="jonej102.InventoryManager" xmlns:jsp="#unknown"></jsp:usebean>
        <table border="1" cellspacing="10">
            <th>UPC</th>
            <th>Long Details</th>
            <th>Short Details</th>
            <th>Price ($)</th>
            <th>Quantity</th>
            <th></th>
            <th></th>
            <c:foreach var="product" items="${inventoryBean.getProductList()}" xmlns:c="#unknown">
                <form action="inventory" method="POST">
                <tr>
                    <td><input type="text" name="upc" value="${product.getUpc()}" size="10" /></td>
                    <td><input type="text" name="longDetails" value="${product.getLongDetails()}" size="50" /></td>
                    <td><input type="text" name="shortDetails" value="${product.getShortDetails()}" size="20" /></td>
                    <td><input type="text" name="price" value="${product.getPrice()}" /></td>
                    <td><input type="text" name="stock" value="${product.getStock()}" size="4" /></td>
                    <td><input type="submit" name="button" value="Update" /></td>
                    <td><input type="submit" name="button" value="Delete" /></td>
                </tr>
                </form>
            </c:foreach>
        </table>
        <br />
        <h1>Manage Inventory</h1>
        <form method="POST" action="inventory">
            UPC: <input type="text" name="upc" size="10" />
            <br /><br />
            Long Details: <input type="text" name="longDetails" size="50" />
            <br /><br />
            Short Details: <input type="text" name="shortDetails" size="20" />
            <br /><br />
            Price: $<input type="text" name="price" />
            <br /><br />
            Stock: <input type="text" name="stock" size="4" />
            <br /><br /><br />
            <input type="submit" name="button" value="Create" />
        </form>
    </body>
</html>


function validateMyForm()
    {
        var valid = true;
        var validationMessage = 'Please correct the following errors:\r\n';
        if (document.getElementById('upc').value.length == 0)
        {
            validationMessage = validationMessage + '  - UPC is missing\r\n';
            valid = false;
        }
        if (document.getElementById('longDetails').value.length == 0)
        {
            validationMessage = validationMessage + '  - Long Details are missing\r\n';
            valid = false;
        }
        if (document.getElementById('shortDetails').value.length != 10)
        {
            validationMessage = validationMessage + '  - Short Details are missing.\r\n';
            valid = false;
        }
        if (document.getElementById('price').value.length == 0)
        {
            validationMessage = validationMessage + '  - Price is missing\r\n';
            valid = false;
        }
        if (document.getElementById('stock').value.length == 0)
        {
            validationMessage = validationMessage + '  - Stock is missing\r\n';
            valid = false;
        }
        if (valid == false)
        {
            alert(validationMessage);
        }
        return valid;
    }
</script>



[edit]Code block added - OriginalGriff[/edit]

What I have tried:

[edit]Left blank by OP - This added to let it post...[/edit]

)</th> <th>Quantity</th> <th></th> <th></th> <c:foreach var="product" items="


{inventoryBean.getProductList()}" xmlns:c="#unknown"> <form action="inventory" method="POST"> <tr> <td><input type="text" name="upc" value="


{product.getUpc()}" size="10" /></td> <td><input type="text" name="longDetails" value="