form表单的检验

从个人经历来看,form表单的检验
1、例子
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

<script type="text/javascript">
function checkForm() {
alert(document.getElementById("selection").value);
alert(document.forma.selection.value);
if (document.forma.selection.value=="0")
window.alert("请选择!");
return false;
}

</script>
</head>
<body>
<form action="servlet" method="post" name="forma" id="forma" onsubmit="return checkForm()">
<table>
<tr>
<td>
<select name="selection" id="selection">
<optgroup label="选择">
<option value="0">
---请选择---
</option>
<option value="1">
选择一
</option>
<option value="2">
选择二
</option>
<option value="3">
选择三
</option>
</optgroup>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>

这样就可以用javascript来检验表单中各个属性是否符合规范,如果某一填写信息不合要求,表单不会提交

2、例子

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

<script type="text/javascript">
function doSubmit(){
alert(document.getElementById("selection").value);
if(document.forma.selection.value=="0"){
window.alert("请选择!");
return false;
}
}
</script>
</head>
<body>
<form action="servlet" method="post" name="forma" id="forma">
<table>
<tr>
<td>
<select name="selection" id="selection">
<optgroup label="选择">
<option value="0">
---请选择---
</option>
<option value="1">
选择一
</option>
<option value="2">
选择二
</option>
<option value="3">
选择三
</option>
</optgroup>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交" onClick="doSubmit()">
</td>
</tr>
</table>
</form>
</body>
</html>

这样的验证可以知道哪些信息不和规范,但是表单还是会提交,实际上起不到检验的效果,因此是一种无效的检验,所以,有效的检验时第一种检验方法,第二种方式可以用作如下情况:当一个jsp页面上有多个按钮,点击不同的按钮跳转到不同的页面,可以这么做,先使用javascript跳转到一个action,然后在action指定跳转的jsp文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值