想验证
1 . 验证城市 当城市的没有被选择(值为默认的city 或者为空)的时候 在这一行后面显示错误提示
2 . 验证时间 当开始时间 和 结束时间为空 或者 开始时间大于结束时间的时候 在这一行后面显示错误提示
这是我的JSP代码query.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html>
<head>
<title>My JSP 'query.jsp' starting page</title>
<script type="text/javascript"> function isPasswd(s){
var patrn=/^(\w){4,12}$/;
if (!patrn.exec(s)) return false;
return true;
} function ResetBt(){
document.all['loginForm'].reset();
document.getElementById("account").focus();
return false;
}function CheckForm(){
if(document.getElementById("account").value.length==0){
alert("对不起!您输入的账号不能为空!");
document.getElementById("account").focus();
return false;
}
if((document.getElementById("account").value.length>16)||(document.getElementById("account").value.length<5)){
alert("对不起!帐号的长度必须为5到16个有效字符!");
document.getElementById("account").focus();
return false;
}
if(isPasswd(document.getElementById("account").value)==false)
{
alert("对不起!帐号只能输入5-16个字母、数字、下划线!");
document.getElementById("account").focus();
return false;
}
if(document.getElementById("pwd").value.length==0){
alert("对不起!密码不能为空!");
document.getElementById("pwd").focus();
return false;
}
if((document.getElementById("pwd").value.length>16)||(document.getElementById("pwd").value.length<5)){
alert("对不起!密码的长度必须为5到16个有效字符!");
document.getElementById("pwd").focus();
return false;
}
if(isPasswd(document.getElementById("pwd").value)==false)
{
alert("对不起!密码只能输入5-16个字母、数字、下划线!");
document.getElementById("pwd").value="";
document.getElementById("pwd").focus();
return false;
}
else{
document.getElementById('loginForm').submit();
return true;
}
}
</script>
</head>
<script language="javascript" type="text/javascript" src="My97DatePicker/WdatePicker.js"> </script>
<body>
<form action="" method="post">
<p>username :
<input name="account" type="text" >
</p>
<p>password :
<label>
<input type="password" name="pwd">
</label>
</p>
<p>
<label>
城市 :
<select name="select">
<option selected>city</option>
<option>北京</option>
<option>上海</option>
<option>武汉</option>
<option>杭州</option>
<option>成都</option>
</select>
</label>
</p>
<p>
开始时间:<input type="text" class="inputText" name="starttime" id="starttime" onClick="new WdatePicker(this,false)"/>
至:<input type="text" class="inputText" name="starttime2" id="starttime2" onClick="new WdatePicker(this,false)"/>
<p>
<input type="submit" name="Submit" value="提交" onClick="return CheckForm();">
<input type="reset" name="Submit2" value="重置" onClick="return ResetBT();">
</p>
</form>
</body>
</html>
1 . 验证城市 当城市的没有被选择(值为默认的city 或者为空)的时候 在这一行后面显示错误提示
2 . 验证时间 当开始时间 和 结束时间为空 或者 开始时间大于结束时间的时候 在这一行后面显示错误提示
这是我的JSP代码query.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html>
<head>
<title>My JSP 'query.jsp' starting page</title>
<script type="text/javascript"> function isPasswd(s){
var patrn=/^(\w){4,12}$/;
if (!patrn.exec(s)) return false;
return true;
} function ResetBt(){
document.all['loginForm'].reset();
document.getElementById("account").focus();
return false;
}function CheckForm(){
if(document.getElementById("account").value.length==0){
alert("对不起!您输入的账号不能为空!");
document.getElementById("account").focus();
return false;
}
if((document.getElementById("account").value.length>16)||(document.getElementById("account").value.length<5)){
alert("对不起!帐号的长度必须为5到16个有效字符!");
document.getElementById("account").focus();
return false;
}
if(isPasswd(document.getElementById("account").value)==false)
{
alert("对不起!帐号只能输入5-16个字母、数字、下划线!");
document.getElementById("account").focus();
return false;
}
if(document.getElementById("pwd").value.length==0){
alert("对不起!密码不能为空!");
document.getElementById("pwd").focus();
return false;
}
if((document.getElementById("pwd").value.length>16)||(document.getElementById("pwd").value.length<5)){
alert("对不起!密码的长度必须为5到16个有效字符!");
document.getElementById("pwd").focus();
return false;
}
if(isPasswd(document.getElementById("pwd").value)==false)
{
alert("对不起!密码只能输入5-16个字母、数字、下划线!");
document.getElementById("pwd").value="";
document.getElementById("pwd").focus();
return false;
}
else{
document.getElementById('loginForm').submit();
return true;
}
}
</script>
</head>
<script language="javascript" type="text/javascript" src="My97DatePicker/WdatePicker.js"> </script>
<body>
<form action="" method="post">
<p>username :
<input name="account" type="text" >
</p>
<p>password :
<label>
<input type="password" name="pwd">
</label>
</p>
<p>
<label>
城市 :
<select name="select">
<option selected>city</option>
<option>北京</option>
<option>上海</option>
<option>武汉</option>
<option>杭州</option>
<option>成都</option>
</select>
</label>
</p>
<p>
开始时间:<input type="text" class="inputText" name="starttime" id="starttime" onClick="new WdatePicker(this,false)"/>
至:<input type="text" class="inputText" name="starttime2" id="starttime2" onClick="new WdatePicker(this,false)"/>
<p>
<input type="submit" name="Submit" value="提交" onClick="return CheckForm();">
<input type="reset" name="Submit2" value="重置" onClick="return ResetBT();">
</p>
</form>
</body>
</html>
首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员 欢迎您:feng8008 | 退出 | 登录 注册 帮助
我的帖子我参与的帖子我的空间我的网摘
CSDNCSDN社区JavaWeb 开发将帖子提前 放进我的网摘 推荐给好友 我要提问 帖子加分 生成帖子 置顶 推荐(加精) 取消推荐(加精) 锁定帖子 移动帖子 取消引用结贴去... 管理菜单 页面风格切换标准风格老版本论坛 请问这个验证怎么写?
加为好友
发送私信
在线聊天
whlusha720
lusha
等级:
发表于:2008-09-02 10:49:55 楼主
想验证
1 . 验证城市 当城市的没有被选择(值为默认的city 或者为空)的时候 在这一行后面显示错误提示
2 . 验证时间 当开始时间 和 结束时间为空 或者 开始时间大于结束时间的时候 在这一行后面显示错误提示
这是我的JSP代码 query.jsp
Java code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html>
<head>
<title>My JSP 'query.jsp' starting page</title>
<script type="text/javascript"> function isPasswd(s){
var patrn=/^(\w){4,12}$/;
if (!patrn.exec(s)) return false;
return true;
} function ResetBt(){
document.all['loginForm'].reset();
document.getElementById("account").focus();
return false;
}function CheckForm(){
if(document.getElementById("account").value.length==0){
alert("对不起!您输入的账号不能为空!");
document.getElementById("account").focus();
return false;
}
if((document.getElementById("account").value.length>16)||(document.getElementById("account").value.length<5)){
alert("对不起!帐号的长度必须为5到16个有效字符!");
document.getElementById("account").focus();
return false;
}
if(isPasswd(document.getElementById("account").value)==false)
{
alert("对不起!帐号只能输入5-16个字母、数字、下划线!");
document.getElementById("account").focus();
return false;
}
if(document.getElementById("pwd").value.length==0){
alert("对不起!密码不能为空!");
document.getElementById("pwd").focus();
return false;
}
if((document.getElementById("pwd").value.length>16)||(document.getElementById("pwd").value.length<5)){
alert("对不起!密码的长度必须为5到16个有效字符!");
document.getElementById("pwd").focus();
return false;
}
if(isPasswd(document.getElementById("pwd").value)==false)
{
alert("对不起!密码只能输入5-16个字母、数字、下划线!");
document.getElementById("pwd").value="";
document.getElementById("pwd").focus();
return false;
}
else{
document.getElementById('loginForm').submit();
return true;
}
}
function checkUser(){
if(document.getElementById("account").value.length==0){
document.getElementById("user").innerHTML="对不起!您输入的账号不能为空!");";
}}
</script>
</head>
<script language="javascript" type="text/javascript" src="My97DatePicker/WdatePicker.js"> </script>
<body>
<form action="" method="post">
<p>username :
<input name="account" type="text" onblur="checkUser()"><span id="user"></span>
</p>
<p>password :
<label>
<input type="password" name="pwd">
</label>
</p>
<p>
<label>
城市 :
<select name="select">
<option selected>city</option>
<option>北京</option>
<option>上海</option>
<option>武汉</option>
<option>杭州</option>
<option>成都</option>
</select>
</label>
</p>
<p>
开始时间:<input type="text" class="inputText" name="starttime" id="starttime" onClick="new WdatePicker(this,false)"/>
至:<input type="text" class="inputText" name="starttime2" id="starttime2" onClick="new WdatePicker(this,false)"/>
<p>
<input type="submit" name="Submit" value="提交" onClick="return CheckForm();">
<input type="reset" name="Submit2" value="重置" onClick="return ResetBT();">
</p>
</form>
</body>
</html>
验证不通过 innerText=错误信息 display "" 通过innerText="" display none
1、判断被选 option 的 text 是不是 city,如果是,就提示错误
document.getElementsByName("select")[0].options[document.getElementsByName("select")[0].selectedIndex].text=="city"2、用 new Date("2008/01/01").getTime() 来获得毫秒,
只要 new Date("第一个日期").getTime() 大于 new Date("第二个日期").getTime()
就提示错误
(必须满足 yyyy/MM/dd 这种格式, yyyy/MM/dd hh:mm/ss 也可以)显示错误的方式就象 4 楼说的那样
账号 密码 已经验证了
我现在只是需要验证 下拉列表值 和 日期
2 3楼的朋友 能不能先把我发的帖子看清楚 ??
这些代码是写在function里面吗 ? 那onclick= 里面怎么写呢 ?
我第一次写验证不是很熟 不好意思 !
只要 new Date("第一个日期").getTime() 大于 new Date("第二个日期").getTime()
就提示错误
(必须满足 yyyy/MM/dd 这种格式, yyyy/MM/dd hh:mm/ss 也可以) 如果我的日期是这种形式呢 ?
"2008-09-03"
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss z");
具体实现看看api找到适合你的转换就可以