<pre class="javascript" name="code">addIp.js:
var inputArray = $("#tBody tr td input");
inputArray.each(function(i,value){
$(this).keyup(function(e){
var key = e.which;
IpFocus($(this),key);
});
});
function IpFocus(obj,key){
//先判断是否大于255 再让其推进后一个焦点
if(key==110||key==39){
if(obj.attr("name")=="beginIp4"){
$("#endIp1").focus();
}else{
obj.next().focus();
}
}else if(key==37){
if(obj.attr("name")=="endIp1"){
$("#beginIp4").focus();
}else{
obj.prev().focus();
}
}else if(obj.val().length==3){
if(parseInt(obj.val())>255||parseInt(obj.val())<1){
art.dialog.alert(obj.val()+"不是一个有效数字,请制定一个介于1和255之间的数值");
obj.focus();
return;
}else{
if(obj.attr("name")=="beginIp4"){
$("#endIp1").focus();
}
obj.next().focus();
}
}
}
function addIp(){
var re = /^([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/;
//拼接字符串
var inputArray = $("#tBody tr td input");
var input1Array = $("#div1 input");
var input2Array = $("#div2 input");
var beginIp = "";
var endIp = "";
var isNullBeginIp="";
var isNullEndIp="";
input1Array.each(function(i,value){
if(i!=3){
beginIp+=$.trim($(this).val())+".";
}else{
beginIp+=$.trim($(this).val());
}
isNullBeginIp+=$.trim($(this).val());
if(isNullBeginIp!=""&&$.trim($(this).val())==""){
art.dialog.alert("起始Ip格式不正确");
return;
}
});
input2Array.each(function(i,value){
if(i!=3){
endIp+=$.trim($(this).val())+".";
}else{
endIp+=$.trim($(this).val());
}
isNullEndIp+=$.trim($(this).val());
if(isNullEndIp!=""&&$.trim($(this).val())==""){
art.dialog.alert("截止IP格式不正确");
return;
}
});
if(isNullBeginIp==""){
art.dialog.alert("起始Ip不能为空!");
return;
}
if(isNullEndIp==""){
art.dialog.alert("截止IP不能为空!");
return;
}
if(!re.test(beginIp)){
art.dialog.alert("起始Ip格式不正确");
return false;
}
if(!re.test(endIp)){
art.dialog.alert("截止IP格式不正确");
return false;
}
//插入Ip
if(isIpSection(beginIp,endIp)=="notEqual"){
art.dialog.alert("起始Ip与截止IP不在同一段Ip范围内");
return;
}else if(isIpSection(beginIp,endIp)=="Error"){
art.dialog.alert("截止IP不能小于起始Ip");
return;
}
if(parseInt(beginIp4[3])>parseInt(endIp4[3])){
art.dialog.alert("起始Ip与截止IP范围不正确");
}
var url = basePath+"attendance/addIp.action";
var paramData = {
"beginIp":beginIp,
"endIp":endIp
};
$.ajax({
url:url,
type:"post",
data:paramData,
dataType:"text",
success:function(data){
if($.trim(data)==""){
art.dialog.alert("添加成功",function(){
art.dialog.close("addIp");
});
}else{
art.dialog.alert("添加失败,请稍后重试!");
}
}
});
}
/**
* 判断两者是否在同一IP段内
* @param beginIp
* @param endIp
*/
function isIpSection(beginIp,endIp){
var beginIpArray = beginIp.split(".");
var endIpArray = endIp.split(".");
var ip1 = (beginIpArray[0]==endIpArray[0]);
var ip2 = (beginIpArray[1]==endIpArray[1]);
var ip3 = (beginIpArray[2]==endIpArray[2]);
var ip4 = (parseInt(beginIpArray[3])<=parseInt(endIpArray[3]));
if(ip1&&ip2&&ip3){
if(!ip4){
return "Error";
}else{
return "";
}
}else{
return "notEqual";
}
}
addIp.jsp
<pre class="javascript" name="code"><pre class="javascript" name="code"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:include page="../../common/taglibs.jsp" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>弹框-考勤设置</title>
<jsp:include page="../../common/flatHead.jsp"></jsp:include>
<link href="${ctx}flat/css/reset.css" rel="stylesheet" type="text/css" />
<link href="${ctx}flat/css/attendance.css" rel="stylesheet" type="text/css" />
<link href="${ctx}flat/plugins/form/skins/form_default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="${ctx}js/common/validate_form.js"></script>
<script type="text/javascript" src="${ctx}js/logined/js_lang_cn.js"></script>
<script type="text/javascript" src="${ctx}js/common/showError.js"></script>
<script type="text/javascript" src="${ctx}js/logined/attendance/addIp.js"></script>
</head>
<body class="formbg">
<div class="elasticFrame formPage">
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="inputTable">
<tbody id="tBody">
<tr>
<th>IP段:</th>
<td><div class="inputIP" id="div1"><input name="beginIp1" maxlength="3" οnkeyup="this.value=this.value.replace(/[^0-9]/g,'')" class="ipClass" id="beginIp1" type="text" />.<input name="beginIp2" id="beginIp2" maxlength="3" οnkeyup="this.value=this.value.replace(/[^0-9]/g,'')" class="ipClass" type="text" />.<input name="beginIp3" id="beginIp3" type="text" maxlength="3" οnkeyup="this.value=this.value.replace(/[^0-9]/g,'')" class="ipClass" />.<input name="beginIp4" id="beginIp4" type="text" maxlength="3" οnkeyup="this.value=this.value.replace(/[^0-9]/g,'')" class="ipClass" /></div>
-
<div class="inputIP" id="div2"><input name="endIp1" id="endIp1" type="text" maxlength="3" οnkeyup="this.value=this.value.replace(/[^0-9]/g,'')" class="ipClass"/>.<input name="endIp2" id="endIp2" type="text" maxlength="3" οnkeyup="this.value=this.value.replace(/[^0-9]/g,'')" class="ipClass"/>.<input name="endIp3" id="endIp3" type="text" maxlength="3" οnkeyup="this.value=this.value.replace(/[^0-9]/g,'')" class="ipClass"/>.<input name="endIp4" id="endIp4" type="text" maxlength="3" οnkeyup="this.value=this.value.replace(/[^0-9]/g,'')" class="ipClass"/></div></td>
</tr>
</tbody>
</table>
<input type="button" style="display:none;" id="a" class="formButton" value="保存" οnclick="addIp()" />
</div>
</body>
</html>