怎样利用javascript对输入的时间进行有效的验证?
2个回答
展开全部
给你发个用来验证身份证 邮箱 等格式的正确与否吧!你仔细 看看!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>正则表达式综合练习</title>
</head>
<body>
<center>
<h1>多种信息格式验证</h1>
<hr>
<form name="myform">
<input type="text" name="email" size="30">
<input type="button" value="邮件地址" onclick="click1()">
<br>
<input type="text" name="ip" size="30">
<input type="button" value="IP 地址" onclick="click2()">
<br>
<input type="text" name="card" size="30">
<input type="button" value="身份证号" onclick="click3()">
<br>
<input type="text" name="date" size=30>
<input type="button" value="日期格式" onclick="click4()">
<br>
<input type="text" name="time" size="30">
<input type="button" value="时间格式" onclick="click5()">
<br>
</form>
</center>
<script type="text/javascript">
function click1()
{
var str=document.myform.email.value;
var reg=/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
if(reg.test(str))
{
alert("格式正确!");
}
else
{
alert("格式错误!");
}
}
function click2()
{
var str=document.myform.ip.value;
var reg=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/;
if(reg.test(str))
{
alert("格式正确!");
}
else
{
alert("格式错误!");
}
}
function click3()
{
var str=document.myform.card.value;
var reg=/^\d{17}(\d|X)$/;
if(reg.test(str))
{
alert("格式正确!");
}
else
{
alert("格式错误!");
}
}
function click4()
{
var str=document.myform.date.value;
var reg=/^\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;
if(reg.test(str))
{
alert("格式正确!");
}
else
{
alert("格式错误!");
}
}
function click5()
{
var str=document.myform.time.value;
var reg=/^([0-1]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$/;
if(reg.test(str))
{
alert("格式正确!");
}
else
{
alert("格式错误!");
}
}
</script>
</body>
</html>
这是一个HTML
文件你考到上面去看看!运行一下 然后在进行更改!应该能帮助你吧~
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>正则表达式综合练习</title>
</head>
<body>
<center>
<h1>多种信息格式验证</h1>
<hr>
<form name="myform">
<input type="text" name="email" size="30">
<input type="button" value="邮件地址" onclick="click1()">
<br>
<input type="text" name="ip" size="30">
<input type="button" value="IP 地址" onclick="click2()">
<br>
<input type="text" name="card" size="30">
<input type="button" value="身份证号" onclick="click3()">
<br>
<input type="text" name="date" size=30>
<input type="button" value="日期格式" onclick="click4()">
<br>
<input type="text" name="time" size="30">
<input type="button" value="时间格式" onclick="click5()">
<br>
</form>
</center>
<script type="text/javascript">
function click1()
{
var str=document.myform.email.value;
var reg=/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
if(reg.test(str))
{
alert("格式正确!");
}
else
{
alert("格式错误!");
}
}
function click2()
{
var str=document.myform.ip.value;
var reg=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/;
if(reg.test(str))
{
alert("格式正确!");
}
else
{
alert("格式错误!");
}
}
function click3()
{
var str=document.myform.card.value;
var reg=/^\d{17}(\d|X)$/;
if(reg.test(str))
{
alert("格式正确!");
}
else
{
alert("格式错误!");
}
}
function click4()
{
var str=document.myform.date.value;
var reg=/^\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;
if(reg.test(str))
{
alert("格式正确!");
}
else
{
alert("格式错误!");
}
}
function click5()
{
var str=document.myform.time.value;
var reg=/^([0-1]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$/;
if(reg.test(str))
{
alert("格式正确!");
}
else
{
alert("格式错误!");
}
}
</script>
</body>
</html>
这是一个HTML
文件你考到上面去看看!运行一下 然后在进行更改!应该能帮助你吧~
展开全部
我以前做的一个例子,给你发过来,js稍微改一下变量就可以了
js:
function checkLoginName() {
var loginName = document.myform.loginName.value;//改成你的变量
if (loginName == "" || loginName == null) {
alert('用户名不能为空!');
return false;
} else {
var url = "action.do?method=checkLoginName&loginName=" +loginName;//跳转的action
send(url);
}
}
var xmlHttp;
function createXmlHttp() {
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("MIcrosoft.XMLHttp");
}
}
function send(url) {
createXmlHttp();
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = processRequest;
xmlHttp.send(null);
}
function processRequest() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var myfont = document.getElementById("name");//这个name跟html页面的name是对应的,不用改
while (myfont.hasChildNodes()) {
myfont.removeChild(myfont.firstChild);
}
var text = document.createTextNode();
text.nodeValue = xmlHttp.responseText;
myfont.appendChild(text);
}
}
}
-----------------------------------------------------------------------------------
action:
public ActionForward checkLoginName(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginDao dao=new LoginDao();
String loginName=request.getParameter("loginName");
loginName=new String(loginName.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(loginName);
int num=dao.check(loginName);
if(num==0){
response.getWriter().println("可用");
response.getWriter().flush();
}else{
response.getWriter().println("已经使用");
response.getWriter().flush();
}
return null;
}
-----------------------------------------------------------------------------------------
dao:
public int check(String loginName){
Session ss=HibernateSessionFactory.getSession();
ss.beginTransaction();
Query q=ss.createQuery("select loginName from Login where loginName='"+loginName+"'");
List list=q.list();
ss.getTransaction().commit();
ss.close();
return list.size();
}
----------------------------------------------------------------------------------------------------------------------
html:
<tr>
<td width="100" align="right">
用户名:
</td>
<td align="left">
<input type="text" name="loginName" />  <input type="button" value="检查用户名" onclick="checkLoginName()">
<font color="red" size="2px" id="name"></font></td>
</tr>
js:
function checkLoginName() {
var loginName = document.myform.loginName.value;//改成你的变量
if (loginName == "" || loginName == null) {
alert('用户名不能为空!');
return false;
} else {
var url = "action.do?method=checkLoginName&loginName=" +loginName;//跳转的action
send(url);
}
}
var xmlHttp;
function createXmlHttp() {
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("MIcrosoft.XMLHttp");
}
}
function send(url) {
createXmlHttp();
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = processRequest;
xmlHttp.send(null);
}
function processRequest() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var myfont = document.getElementById("name");//这个name跟html页面的name是对应的,不用改
while (myfont.hasChildNodes()) {
myfont.removeChild(myfont.firstChild);
}
var text = document.createTextNode();
text.nodeValue = xmlHttp.responseText;
myfont.appendChild(text);
}
}
}
-----------------------------------------------------------------------------------
action:
public ActionForward checkLoginName(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginDao dao=new LoginDao();
String loginName=request.getParameter("loginName");
loginName=new String(loginName.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(loginName);
int num=dao.check(loginName);
if(num==0){
response.getWriter().println("可用");
response.getWriter().flush();
}else{
response.getWriter().println("已经使用");
response.getWriter().flush();
}
return null;
}
-----------------------------------------------------------------------------------------
dao:
public int check(String loginName){
Session ss=HibernateSessionFactory.getSession();
ss.beginTransaction();
Query q=ss.createQuery("select loginName from Login where loginName='"+loginName+"'");
List list=q.list();
ss.getTransaction().commit();
ss.close();
return list.size();
}
----------------------------------------------------------------------------------------------------------------------
html:
<tr>
<td width="100" align="right">
用户名:
</td>
<td align="left">
<input type="text" name="loginName" />  <input type="button" value="检查用户名" onclick="checkLoginName()">
<font color="red" size="2px" id="name"></font></td>
</tr>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询