从个人经历来看,form表单的检验
1、例子
这样就可以用javascript来检验表单中各个属性是否符合规范,如果某一填写信息不合要求,表单不会提交
2、例子
这样的验证可以知道哪些信息不和规范,但是表单还是会提交,实际上起不到检验的效果,因此是一种无效的检验,所以,有效的检验时第一种检验方法,第二种方式可以用作如下情况:当一个jsp页面上有多个按钮,点击不同的按钮跳转到不同的页面,可以这么做,先使用javascript跳转到一个action,然后在action指定跳转的jsp文件
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文件