<%@ 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>
<script type="text/javascript">
function checkData(){
//检查是否是数字
var txt=document.forms[0].number.value;
if(txt.search("^\\d+(\\.\\d+)*$" )!=0){
alert("请输入一个数字!");
document.forms[0].number.select();
return false;
}
//查是否是整数
txt = document.forms[0].intnumber.value;
if(txt.search("^-?\\d+$")!=0){
alert("请输入一个整数!");
document.forms[0].intnumber.select();
return false;
}
//检查E-mail是否合法
txt=document.forms[0].email.value;
if(txt.search("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$")!=0){
alert("请输入正确的E-mail地址!");
document.forms[0].email.select();
return false;
}
alert("检查通过!");
return ture;
}
//OnSubmit="return checkData()"
</script>
<base href="<%=basePath%>">
<title>正则表达式</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">
-->
</head>
<body>
<form action="" method="post" onsubmit="return checkData()">
<br>请输入一个数字:<input type="text" name="number">
<br>请输入一个整数:<input type="text" name="intnumber">
<br>请输入电子邮件E-mail:<input type="text" name="email">
<br><br>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
上面的只是做了数据校验,比如只能输入数字,只能输入邮箱,还没有对输入的字符串的长度做出限制。。