验证jsp页面文本框 [英] Validate jsp page textboxes

查看:68
本文介绍了验证jsp页面文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个需要帮助验证的jsp页面。页面上有5个文本框。 2是文本(longDetails,shortDetails),3是整数(UPC,Price,Quantity)。他们都需要进行验证,以确保表单未提交无效数据。

我的页面代码和我的脚本请告诉我我做错了什么。



 <%@   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 = text / html; charset = UTF-8 >
< title > 广告资源< / title >
< / head >
< / script >
< body >
< span class =code-keyword>< h1 > 查看广告资源< / h1 >
< jsp:usebean id = inventoryBean class = jonej102.InventoryManager xmlns:jsp = #unknown > < / jsp:usebean >
< table < span class =code-attribute> border = 1 cellspacing = 10 >
< th > UPC < / th >
< th > 详细信息< / th >
< th > 简短详情< / th >
< th > 价格($)< / th >
< th > 数量< / th >
< th > < / th >
< th > < / th >
< c:foreach var = product items = $ {inventoryBean.getProductList()} xmlns:c = #unknown >
< 表单 action = < span class =code-keyword> inventory 方法 = POST >
< tr >
< td > < input type = text name = upc value = $ {product.getUpc()} size = 10 / > < span class =code-keyword>< / td >
< td > < input 类型 = text 名称 = longDetails value = $ {product.getLongDetails()} 大小 = 50 / > < / td >
< ; td > < 输入 type = text 名称 = shortDetails value = $ {product.getShortDetails()} size = 20 / > < / td >
< td > < input type = text name = price value = $ {product.getPrice()} / > < / td >
< span class =code-keyword><
td > < 输入 type = text name = sto ck value = $ {product.getStock()} size = 4 < span class =code-keyword> / > < / td >
< td > < input type = 提交 名称 = 按钮 value = 更新 / > < / td >
< td > < input 类型 = 提交 名称 = bu tton value = 删除 / > < / td >
< / tr >
< / form >
< / c:foreach >
< / table >
< br / >
< h1 > 管理广告资源< / h1 >
< form 方法 = POST action = 库存 >
UPC:< 输入 类型 = text name = upc size = 10 / >
< br / > < br / >
详细信息:< input type = text < span class =code-attribute> name = longDetails size = 50 / >
< br / > < br / >
简短详情:< input type = text name = shortDetails size = 20 < span class =code-keyword> / >
< br / > < br / >
价格:$ < 输入 < span class =code-attribute> type = text 名称 = 价格 / >
< br / > < br / < span class =code-keyword>>
库存:< 输入 type = text name = stock size = 4 < span class =code-keyword> / >
< br / > < br / > < br / >
< 输入 类型 = 提交 名称 = 按钮 = 创建 / >
< / form >
< / body >
< span class =code-keyword>< / html >


函数validateMyForm()
{
var valid = true;
var validationMessage ='请更正以下错误:\\\\ n';
if(document.getElementById('upc')。value.length == 0)
{
validationMessage = validationMessage +' - UPC缺失\\\\ n';
有效= false;
}
if(document.getElementById('longDetails')。value.length == 0)
{
validationMessage = validationMessage +' - Long Details is missing\r \\ \
';
有效= false;
}
if(document.getElementById('shortDetails')。value.length!= 10)
{
validationMessage = validationMessage +' - 缺少详细信息。\ r \\\
';
有效= false;
}
if(document.getElementById('price')。value.length == 0)
{
validationMessage = validationMessage +' - 价格缺失\\\ N';
有效= false;
}
if(document.getElementById('stock')。value.length == 0)
{
validationMessage = validationMessage +' - 库存缺失\\\ N';
有效= false;
}
if(valid == false)
{
alert(validationMessage);
}
返回有效;
}
< / 脚本 >





[edit]已添加代码块 - OriginalGriff [/ edit]



我的尝试:



OP留空 - 这个添加让它发布... [/ edit]

解决方案

< ; / th >
< th > 数量< ; / th >
< th > < / th >
< th > < / th >
< < span class =code-leadattribute> c:foreach var = product items =


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


{product.getUpc()} size = 10 / > < < span class =code-leadattribute> / td >
< td > < input type < span class =code-keyword> = 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="


这篇关于验证jsp页面文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆