js日期/时间比较函数,以及js校验表单后提交表单的三种方法,表单验证,以及三种结合

<pre class="html" name="code">

js日期比较(yyyy-mm-dd)

function duibi(a, b) {
var arr = a.split("-");
var starttime = new Date(arr[0], arr[1], arr[2]);
var starttimes = starttime.getTime();

var arrs = b.split("-");
var lktime = new Date(arrs[0], arrs[1], arrs[2]);
var lktimes = lktime.getTime();

if (starttimes >= lktimes) {

alert('开始时间大于离开时间,请检查');
return false;
}
else
return true;

}


js时间比较(yyyy-mm-dd hh:mi:ss)


function comptime() {
var beginTime = "2009-09-21 00:00:00";
var endTime = "2009-09-21 00:00:01";
var beginTimes = beginTime.substring(0, 10).split('-');
var endTimes = endTime.substring(0, 10).split('-');

beginTime = beginTimes[1] + '-' + beginTimes[2] + '-' + beginTimes[0] + ' ' + beginTime.substring(10, 19);
endTime = endTimes[1] + '-' + endTimes[2] + '-' + endTimes[0] + ' ' + endTime.substring(10, 19);

alert(beginTime + "aaa" + endTime);
alert(Date.parse(endTime));
alert(Date.parse(beginTime));
var a = (Date.parse(endTime) - Date.parse(beginTime)) / 3600 / 1000;
if (a < 0) {
alert("endTime小!");
} else if (a > 0) {
alert("endTime大!");
} else if (a == 0) {
alert("时间相等!");
} else {
return 'exception'
}
}
 
 


 

 

--------------------------------js校验表单后提交表单的三种方法----------------------------------

第一种:

<script type="text/javascript">
         function check(form) {

          if(form.userId.value=='') {
                alert("请输入用户帐号!");
                form.userId.focus();
                return false;
           }
       if(form.password.value==''){
                alert("请输入登录密码!");
                form.password.focus();
                return false;
         }
         return true;
         }
</script>
 
<form action="login.do?act=login" method="post">
用户帐号
  <input type=text name="userId" size="18" value="" >
<br>
 登录密码      
<input type="password" name="password" size="19" value=""/>      
 <input type=submit name="submit1" value="登陆" onclick="return check(this.form)">  

</form>   


第二种:

<script type="text/javascript">
function check(form) {

if(form.userId.value=='') {
alert("请输入用户帐号!");
form.userId.focus();
return false;
}
if(form.password.value==''){
alert("请输入登录密码!");
form.password.focus();
return false;
}
return true;
}
</script>


<form action="login.do?act=login" method="post" onsubmit="return check(this)">
用户帐号
<input type=text name="userId" size="18" value="" >
<br>
登录密码 
<input type="password" name="password" size="19" value=""/> 
<input type=submit name="submit1" value="登陆"> 

</form> 


 

第三种:

<script type="text/javascript">
         function check(form) {

          if(form.userId.value=='') {
                alert("请输入用户帐号!");
                form.userId.focus();
                return false;
           }
       if(form.password.value==''){
                alert("请输入登录密码!");
                form.password.focus();
                return false;
         }

          document.myform.submit();
}
</script>
 
<form action="login.do?act=login" name="myform" method="post">
用户帐号
  <input type=text name="userId" size="18" value="" >
<br>
 登录密码      
<input type="password" name="password" size="19" value=""/>      
<input type=button name="submit1" value="登陆" onclick="check(this.form)">  

</form> 


表单验证:

<script type="text/javascript" src="${path}/js/custom/validator.js"></script>

 

三种结合:

<script type="text/javascript" src="${path}/js/custom/validator.js"></script>


 

<script language="javascript">

	function check(form) {
		//alert(form.open_date.value);
		var beginTime = form.open_date.value;
	    var endTime = form.bid_date.value;
	    var beginTimes = beginTime.substring(0, 10).split('-');
	    var endTimes = endTime.substring(0, 10).split('-');

	    beginTime = beginTimes[1] + '-' + beginTimes[2] + '-' + beginTimes[0] + ' ' + beginTime.substring(10, 19);
	    endTime = endTimes[1] + '-' + endTimes[2] + '-' + endTimes[0] + ' ' + endTime.substring(10, 19);

	    //alert(beginTime + "aaa" + endTime);
	   // alert(Date.parse(endTime));
	   // alert(Date.parse(beginTime));
	    var a = (Date.parse(endTime) - Date.parse(beginTime)) / 3600 / 1000;
	    if (a < 0) {
	        alert("评标时间必须晚于开标时间!");
	        return false;
	    } else if (a > 0) {
	        //alert("endTime大!");
		//document.myform.submit();
	    } else if (a == 0) {
	        alert("评标时间必须晚于开标时间!");
	        return false;
	    } else {
	    	return false;
	    }
	    return true;
	}
		     </script>
 
------------------表单----------------
<form action="${path}/AmAppliaction_add.do"
			method="post" id="myform" name="myform" onSubmit="return Validator.Validate(this,3)">
				<table class="table" cellspacing="0" cellpadding="2" width="99%"
				align="center" border="0">


 

	<input label="开标时间" id="open_date" name="open_date" value="${open_date}"  dataType="Require" msg="开标时间不能为空"
												class="Wdate" size="10" readonly="readonly" style="height:15px;width: 184px;"
												 onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})"  />
<td class="td_bg"  align="left" colspan="2">
							<input type="submit" name="submit1" value="提交" onclick="check(this.form)">  
							<s:reset value="重置" cssClass="btn"></s:reset>
						</td>
</form>

--------------------------验证会先比较时间-------------------然后FORM提交触发JS表单验证------------------------



你可能感兴趣的:(JavaScript,html,json,函数,表单)