javascript判断提交的表单,程序如下:
<%--
Document : index
Created on : 2008-7-25, 8:55:46
Author : Administrator
--%><%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户注册</title>
</head>
<body>
<script language="javascript">
function IsDigit(cCheck){
return (('0'<=cCheck)&&(cCheck<='9'));
}
function IsPlpha(cCheck){
return ((('a'<=cCheck)&&(cCheck<='z'))||(('A'<=cCheck)&&(cCheck<='Z')));
}
function IsVaild(){
var struserName=reg.UserName.value;
for(nIndex=0;nIndex<struserName;nIndex++){
if(!(IsDigit(cCheck)||IsAlpha(cCheck))){
return false;
}
}
return true;
}
function chkEmail(str){
return str.search(/[\w\-]{1,}@[\w\-]{1,}.[\w\-]{1,}/)==0?true:false;
}
function docheck(){
if(reg.UserName.value==""){
alert("请填写用户名");
return false;
}
else if(!IsValid()){
alert("用户名只能为数字或者是字母");
return false;
}
else if(reg.UserPassword.value==""){
alert("请填写密码");
return false;
}
else if(reg.UserPassword.value!=reg.CUserPassword.value){
alert("两次密码不一致");
return false;
}
else if(reg.NickName==""){
alert("填写昵称");
return false;
}
else if(reg.Email.value==""){
alert("填写邮箱");
return false;
}
else if(!chkEmail(reg.Email.value)){
alert("请填写有效的邮箱");
return false;
}
else{
return true;
}
}
</script>
<h1 align="center">用户注册</h1>
<div align="center">
<form name="reg" action="user_add.jsp" method="post" target="_self" onSubmit="return docheck()">
<table width="90%" border="1">
<tr>
<td width="50%" align="right" height="25">请输入要注册的用户名:</td>
<td width="50%" height="25"> <input type="text" name="UserName"><br><font color="red"> 用户名只能由数字和字母组成</font></td>
</tr>
<tr>
<td align="right" height="25" width="50%">请输入密码:</td>
<td width="50%" height="25"> <input type="password" name="UserPassword"></td>
</tr>
<tr>
<td width="50%" align="right" height="25">请输入确认密码:</td>
<td width="50%" height="25"> <input type="password" name="CUserPassword"></td>
</tr>
<tr>
<td width="50%" align="right" height="25">请输入昵称:</td>
<td width="50%" height="25"> <input type="text" name="NichName"></td>
</tr>
<tr>
<td width="50%" align="right" height="25">请选择性别:</td>
<td width="50%" height="25"> <input type="radio" name="Sex" value="0" checked>男 <input type="radio" name="Sex" value="1">女</td>
</tr>
<tr>
<td width="50%" align="right" height="25">请输入Email地址:</td>
<td width="50%" height="25"> <input type="text" name="Email"></td>
</tr>
</table>
<p>
<input type="submit" name="sub" value="注册">
<input type="reset" name="res" value="重填">
</p>
</form>
</div>
</body>
</html>
我在运行表单的时候,JavaScript就没有起到作用啊,不晓得是那里出错了啊?
<%--
Document : index
Created on : 2008-7-25, 8:55:46
Author : Administrator
--%><%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户注册</title>
</head>
<body>
<script language="javascript">
function IsDigit(cCheck){
return (('0'<=cCheck)&&(cCheck<='9'));
}
function IsPlpha(cCheck){
return ((('a'<=cCheck)&&(cCheck<='z'))||(('A'<=cCheck)&&(cCheck<='Z')));
}
function IsVaild(){
var struserName=reg.UserName.value;
for(nIndex=0;nIndex<struserName;nIndex++){
if(!(IsDigit(cCheck)||IsAlpha(cCheck))){
return false;
}
}
return true;
}
function chkEmail(str){
return str.search(/[\w\-]{1,}@[\w\-]{1,}.[\w\-]{1,}/)==0?true:false;
}
function docheck(){
if(reg.UserName.value==""){
alert("请填写用户名");
return false;
}
else if(!IsValid()){
alert("用户名只能为数字或者是字母");
return false;
}
else if(reg.UserPassword.value==""){
alert("请填写密码");
return false;
}
else if(reg.UserPassword.value!=reg.CUserPassword.value){
alert("两次密码不一致");
return false;
}
else if(reg.NickName==""){
alert("填写昵称");
return false;
}
else if(reg.Email.value==""){
alert("填写邮箱");
return false;
}
else if(!chkEmail(reg.Email.value)){
alert("请填写有效的邮箱");
return false;
}
else{
return true;
}
}
</script>
<h1 align="center">用户注册</h1>
<div align="center">
<form name="reg" action="user_add.jsp" method="post" target="_self" onSubmit="return docheck()">
<table width="90%" border="1">
<tr>
<td width="50%" align="right" height="25">请输入要注册的用户名:</td>
<td width="50%" height="25"> <input type="text" name="UserName"><br><font color="red"> 用户名只能由数字和字母组成</font></td>
</tr>
<tr>
<td align="right" height="25" width="50%">请输入密码:</td>
<td width="50%" height="25"> <input type="password" name="UserPassword"></td>
</tr>
<tr>
<td width="50%" align="right" height="25">请输入确认密码:</td>
<td width="50%" height="25"> <input type="password" name="CUserPassword"></td>
</tr>
<tr>
<td width="50%" align="right" height="25">请输入昵称:</td>
<td width="50%" height="25"> <input type="text" name="NichName"></td>
</tr>
<tr>
<td width="50%" align="right" height="25">请选择性别:</td>
<td width="50%" height="25"> <input type="radio" name="Sex" value="0" checked>男 <input type="radio" name="Sex" value="1">女</td>
</tr>
<tr>
<td width="50%" align="right" height="25">请输入Email地址:</td>
<td width="50%" height="25"> <input type="text" name="Email"></td>
</tr>
</table>
<p>
<input type="submit" name="sub" value="注册">
<input type="reset" name="res" value="重填">
</p>
</form>
</div>
</body>
</html>
我在运行表单的时候,JavaScript就没有起到作用啊,不晓得是那里出错了啊?
楼主【qunqun886】截止到2008-07-25 10:53:48的历史汇总数据(不包括此帖):
发帖的总数量:4 发帖的总分数:80 每贴平均分数:20
回帖的总数量:1 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:4 结贴的总分数:80
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:0.00 % 无满意结分率:0.00 %
敬礼!
<form name="reg" action="user_add.jsp" method="post" target="_self" >
把这两个地方先改改 看你的脚本能起作用不?
alert("用户名只能为数字或者是字母");
return false;
} function IsVaild(){
var struserName=reg.UserName.value;
for(nIndex=0;nIndex <struserName;nIndex++){
if(!(IsDigit(cCheck) ¦ ¦IsAlpha(cCheck))){
return false;
}
}
return true;
} 这2个方法名不一致
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户注册 </title>
</head>
<body>
<h1 align="center">用户注册 </h1>
<div align="center"> <script language="javascript">
function IsDigit(cCheck){
return (('0' <=cCheck)&&(cCheck <='9'));
} function IsPlpha(cCheck){
return ((('a' <=cCheck)&&(cCheck <='z'))||(('A' <=scCheck)&&(cCheck <='Z')));
}
function IsVaild(){
var struserName=reg.UserName.value;
for(nIndex=0;nIndex<struserName;nIndex++){
if(!(IsDigit(cCheck)||IsPlpha(cCheck))){
return false;
}
}
return true;
}
function chkEmail(str){
return str.search(/[\w\-]{1,}@[\w\-]{1,}.[\w\-]{1,}/)==0?true:false;
}
function docheck(){
if(reg.UserName.value==""){
alert("请填写用户名");
return false;
}else if(!IsVaild()){
alert("用户名只能为数字或者是字母");
return false;
}
else if(reg.UserPassword.value==""){
alert("请填写密码");
return false;
}
else if(reg.UserPassword.value!=reg.CUserPassword.value){
alert("两次密码不一致");
return false;
}
else if(reg.NickName==""){
alert("填写昵称");
return false;
}
else if(reg.Email.value==""){
alert("填写邮箱");
return false;
}
else if(!chkEmail(reg.Email.value)){
alert("请填写有效的邮箱");
return false;
}
else{
return true;
}
}
</script>
<form name="reg" action="user_add.jsp" method="post" onSubmit="return docheck();">
<table width="90%" border="1">
<tr>
<td width="50%" align="right" height="25">请输入要注册的用户名: </td>
<td width="50%" height="25"> <input type="text" name="UserName"> <br> <font color="red"> 用户名只能由数字和字母组成 </font> </td>
</tr>
<tr>
<td align="right" height="25" width="50%">请输入密码: </td>
<td width="50%" height="25"> <input type="password" name="UserPassword"> </td>
</tr>
<tr>
<td width="50%" align="right" height="25">请输入确认密码: </td>
<td width="50%" height="25"> <input type="password" name="CUserPassword"> </td>
</tr>
<tr>
<td width="50%" align="right" height="25">请输入昵称: </td>
<td width="50%" height="25"> <input type="text" name="NichName"> </td>
</tr>
<tr>
<td width="50%" align="right" height="25">请选择性别: </td>
<td width="50%" height="25"> <input type="radio" name="Sex" value="0" checked>男 <input type="radio" name="Sex" value="1">女 </td>
</tr>
<tr>
<td width="50%" align="right" height="25">请输入Email地址: </td>
<td width="50%" height="25"> <input type="text" name="Email"> </td>
</tr>
</table>
<p>
<input type="submit" name="sub" value="注册">
<input type="reset" name="res" value="重填">
</p>
</form>
</div>
</body>
</html> 我测试过了 现在应该可以了