/**
* 提交表单,隐藏url中的参数,替换掉location.href = action?param1=1¶m2=10
* url 跳转的Action
* params数组,数组中每个对象需要有两个属性 key和value 分别对应Action中传递的参数名和对应的值
* 使用样例:
* var params = [];
* var param1 = {};
* param1.key = "param1";
* param1.value = 100;
* var param2 = {};
* param2.key = "param2";
* param2.value = 200;
* params.push(param1);
* params.push(param2);
* formJumpFun("myAction", params);
*/
function formJumpFun(url, params, target){
var muyeForm = document.createElement("form");
muyeForm.method="post" ;
muyeForm.action = url;
if(target == "_blank"){
muyeForm.target = target;
}
for(var i = 0; i < params.length; i++){
var paramTmp = params[i];
var inputTmp = document.createElement("input") ;
inputTmp.setAttribute("name", paramTmp.key) ;
inputTmp.setAttribute("value", paramTmp.value);
muyeForm.appendChild(inputTmp) ;
}
document.body.appendChild(muyeForm);
//TODO IE有点问题
// if(target != undefined && target != null){
// muyeForm.target = target;
// if(target != "_blank"){
// window.open("about:blank", target ,'status=yes,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,width=600,height=400,top=0,left=220');
// }
// }
muyeForm.submit();
document.body.removeChild(muyeForm);
};
//====================================================一下公共校验函数
function get_Cookie(name) {
var start = document.cookie.indexOf(name + '=');
var len = start + name.length + 1;
if ((!start) && (name != document.cookie.substring(0, name.length)))
return null;
if (start == -1)
return null;
var end = document.cookie.indexOf(';', len);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(len, end));
}
function set_Cookie(name, value, expires) {
var today = new Date();
today.setTime(today.getTime());
if (expires) {
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date(today.getTime() + (expires));
document.cookie = name + "=" + escape(value)
+ ((expires) ? ";expires=" + expires_date.toGMTString() : "");
}
function delete_Cookie(name, path, domain) {
if (Get_Cookie(name)) {
document.cookie = name + '=' + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
}
var languageSet = "languageSet";
var sysLan = get_Cookie(languageSet);
if (null == sysLan) {
sysLan = "zh";
}
function getLanguage(language) {
sysLan = language;
}
var wakeMsg1;
var wakeMsg2;
var wakeMsg3;
var wakeMsg4;
var wakeMsg5;
var wakeMsg6;
var wakeMsg7;
var wakeMsg8;
var wakeMsg9;
var wakeMsg10;
var wakeMsg11;
var wakeMsg12;
var wakeMsg13;
var wakeMsg14;
var wakeMsg15;
var wakeMsg16;
var wakeMsg17;
var usrCheckMsg1 = "只能为字母开头,由字母、数字或下划线组成!";
var usrCheckMsg2 = "长度不能小于3!";
var usrCheckMsg3 = "长度不能大于20!";
var usrCheckMsg4 = "不能以下划线作为开头或结尾!";
var usrCheckMsg5 = "不能全为数字!";
var pwdCheckMsg1 = "长度不能小于6!";
var pwdCheckMsg2 = "长度不能大于32!";
var pwdCheckMsg3 = "只能由字母、数字或下划线组成!";
var pwdCheckMsg4 = "不能由同一个字母、数字或下划线组成!";
var usrNameAndPwdMsg1 = "不能与用户名相同!";
if (sysLan.indexOf("en") != -1) {
wakeMsg1 = " can not contain chinese characters!";
wakeMsg2 = " contain illegal characters ";
wakeMsg3 = "Please enter ";
wakeMsg4 = "can only be number or letters of the alphabet!";
wakeMsg5 = "can only be a positive integer!";
wakeMsg6 = " length can not bigger than ";
wakeMsg7 = "Please select at least one ";
wakeMsg8 = " to delete!";
wakeMsg9 = "Do you want to delete ";
wakeMsg10 = "?";
wakeMsg11 = " to ";
wakeMsg12 = "Do you want to ";
wakeMsg13 = "Delete success!";
wakeMsg14 = "Request fail,error code:";
wakeMsg15 = "can not have blank!";
wakeMsg16 = "illegal IP format!";
wakeMsg17 = "illegal httpurl format!";
usrCheckMsg1 = " only by letters, numbers or underline and must be start with letter!";
usrCheckMsg2 = " length cannot be less than 3!";
usrCheckMsg3 = " length cannot be bigger than 20!";
usrCheckMsg4 = " cannot begin or end with the underline!";
usrCheckMsg5 = " cannot be fully digital!";
pwdCheckMsg1 = " length cannot be less than 6!";
pwdCheckMsg2 = " length cannot be bigger than 32!";
pwdCheckMsg3 = " can only consist of letters, numbers or underline!";
pwdCheckMsg4 = " cannot be performed by the same letters, figures or underline!";
usrNameAndPwdMsg1 = "The password cannot be the same with the username!";
} else {
wakeMsg1 = "不能包含汉字!";
wakeMsg2 = "含有非法字符";
wakeMsg3 = "请输入";
wakeMsg4 = "只能为数字或英文字母!";
wakeMsg5 = "只能为正整数!";
wakeMsg6 = "长度不能超过";
wakeMsg7 = "请至少选择一个";
wakeMsg8 = "进行删除!";
wakeMsg9 = "确定要删除";
wakeMsg10 = "吗?";
wakeMsg11 = "进行";
wakeMsg12 = "确定要";
wakeMsg13 = "删除成功!";
wakeMsg14 = "请求失败,错误码:";
wakeMsg15 = "不能包含空格!";
wakeMsg16 = "IP格式不合法!";
wakeMsg17 = "httpurl格式不合法";
}
/**
* 校验用户名
* 规则:用户名只能由字母、数字或下划线组成,长度3~20,不能全为数字。
* @param entityName 实体名即参数名
* @param userName 用户名
* @param isAutoAlert 是否自动提示,true则自动提示信息,false则不提示,直接返回false
* @return 校验通过返回true 否则返回 false;
*/
function checkUserName(entityName, userName, isAutoAlert) {
if (null == userName || userName.length < 3) {
if (isAutoAlert) {
alert(entityName + usrCheckMsg2);
}
return false;
}
if (userName.length > 20) {
if (isAutoAlert) {
alert(entityName + usrCheckMsg3);
}
return false;
}
if (userName.indexOf('_') == 0 || userName.indexOf('_') == userName.length) {
if (isAutoAlert) {
alert(entityName + usrCheckMsg4);
}
return false;
}
var patrn = /^[0-9]{1,20}$/;
if (patrn.exec(userName)) {
if (isAutoAlert) {
alert(entityName + usrCheckMsg5);
}
return false;
}
var patrn1 = /^[a-zA-Z]{1}([a-zA-Z0-9]|[_]){4,19}$/;
if (!patrn1.exec(userName)) {
if (isAutoAlert) {
alert(entityName + usrCheckMsg1);
}
return false;
}
return true;
}
/**
* 校验用户密码
* 规则:密码只能由6~32位字母、数字或下划线组成,密码不能与用户名相同,密码不能全部由同一个字母、数字或下划线组成。
* @param entityName 实体名即参数名
* @param userPWD 用户密码
* @param isAutoAlert 是否自动提示,true则自动提示信息,false则不提示,直接返回false
* @return 校验通过返回true 否则返回 false;
*/
function checkUserPassword(entityName, userPWD, isAutoAlert) {
if (null == userPWD || userPWD.length < 6) {
if (isAutoAlert) {
alert(entityName + pwdCheckMsg1);
}
return false;
}
if (userPWD.length > 32) {
if (isAutoAlert) {
alert(entityName + pwdCheckMsg2);
}
return false;
}
var patrn1 = /^([a-zA-Z0-9]|[_]){6,32}$/;
if (!patrn1.exec(userPWD)) {
if (isAutoAlert) {
alert(entityName + pwdCheckMsg3);
}
return false;
}
var temp = userPWD.charAt(0);
var temp1 = "";
var count = 0;
for ( var i = 0; i < userPWD.length; i++) {
temp1 = userPWD.charAt(i);
if (temp1 == temp) {
count++;
}
}
if (count == userPWD.length) {
if (isAutoAlert) {
alert(entityName + pwdCheckMsg4);
}
return false;
}
return true;
}
/**
* 校验用户名和密码是否相同
* 规则:密码只能由6~32位字母、数字或下划线组成,密码不能与用户名相同,密码不能全部由同一个字母、数字或下划线组成。
* @param userName 用户名
* @param userPWD 用户密码
* @return 相同返回true 否则返回 false;
*/
function checkUserNameAndPwdIsSame(userName, userPWD) {
if (userName == userPWD) {
alert(usrNameAndPwdMsg1);
return true;
}
return false;
}
/**
* 检查是否含有非数字或字母
* @param str 要检查的字符串
* @return 0:含有 1:全部为数字或字母。
*/
function checkCharOrDigital(str) {
var strSource = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.()& ";
var ch;
var i;
var temp;
for (i = 0; i <= (str.length - 1); i++) {
ch = str.charAt(i);
temp = strSource.indexOf(ch);
if (temp == -1) {
return 0;
}
}
return 1;
}
/**
* 去掉左右空格
*
* @return
*/
String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
};
// 函数名:checkData
// 功能介绍:检查Form对象
// 参数说明:
//1 obj:要检查的对象,
//2 objErr:返回的错误提示显示对象
//3 name:要检查的对象的中文名称,
//4 length:检查的对象的长度(<0不检查),
//5 notnull:为true则检查非空,
//6 notSpecChar1:为true则检查有无特殊字符(<>),
//7 notSpecChar2:为true则检查有无特殊字符(`~^&*=|'\"<>\\?%#),
//8 notChinese:为true则检查有无中文字符,
//9 numOrLetter:为true则检查只能为数字或英文字母,
//10 isNumber:为true则检查只能为正整数,
//11 isHaveBlank:为true则不能含有空格
//12 isIP:为true则检查是否符合ip格式
//13 isHttpUrl:为true则检查是否符合httpurl格式
// 返回值:false:检查不通过 true:全部为可Input字符
function checkData(obj, objErr, name, length, notnull, notSpecChar1, notSpecChar2, notChinese,
numOrLetter, isNumber, isHaveBlank, isIP, isHttpUrl) {
// 检查对象
if (!obj) {
alert(obj + " is not object!");
return false;
}
// 检查对象
if (!objErr) {
alert(objErr + " is not object!");
return false;
}
objErr.html("");
var msg;
// 检测汉字
if (notChinese && (checkChinese(obj.val()) != 1)) {
msg = "*" + name + wakeMsg1;
objErr.html(msg);
obj.focus();
return false;
}
// 检测特殊字符< >
if (notSpecChar1) {
if (!checktextareaInput(obj.val())) {
var noinput = " < > ";
msg = "*" + name + wakeMsg2 + "(" + noinput + ")!";
objErr.html(msg);
obj.focus();
return false;
}
}
// 检测特殊字符`~^&*=|'\"<>\\?%#
if (notSpecChar2) {
if (!checkInputChr(obj.val())) {
var notinput = "`~^&*=|'\"<>\\?%#";
msg = "*" + name + wakeMsg2 + "(" + notinput + ")!";
objErr.html(msg);
obj.focus();
return false;
}
}
// 检测长度
if (length >= 0 && (checkLength(obj.val()) > length)) {
msg = "*" + name + wakeMsg6 + length + "!";
objErr.html(msg);
obj.focus();
return false;
}
// 检测非空
if (notnull && obj.val().trim() == "") {
msg = "*" + wakeMsg3 + name + "!";
objErr.html(msg);
obj.focus();
return false;
}
// 检测只能为数字或英文字母
re = /[\W_]/;
if (numOrLetter && re.exec(obj.val())) {
msg = "*" + name + wakeMsg4;
objErr.html(msg);
obj.focus();
return false;
}
// 检测只能为正整数
re = /[\D_]/;
if (isNumber && re.exec(obj.val())) {
msg = "*" + name + wakeMsg5;
objErr.html(msg);
obj.focus();
return false;
}
//是否包含空格
if (isHaveBlank && checkHaveBlank(obj.val())) {
msg = "*" + name + wakeMsg15;
objErr.html(msg);
obj.focus();
return false;
}
//是否符合ip格式
if (isIP&&!checkIP(obj.val())) {
msg = "*" + name + wakeMsg16;
objErr.html(msg);
obj.focus();
return false;
}
//是否符合httpurl格式
if (isHttpUrl&&!isHttpURL(obj.val())) {
msg = "*" + name + wakeMsg17;
objErr.html(msg);
obj.focus();
return false;
}
return true;
}
// 函数名:checktextareaInput
// 功能介绍:检查是否含有非Input字符<,>
// 参数说明:要检查的字符串
// 返回值:false:含有 true:全部为可Input字符
function checktextareaInput(str) {
var notareainput = "<>";
var i;
for (i = 0; notareainput != null && i < notareainput.length; i++) {
if (str.indexOf(notareainput.charAt(i)) >= 0) {// 若有
return false;
}
}
return true;
}
// 函数名:checkChinese
// 功能介绍:检查是否含有汉字
// 参数说明:要检查的字符串
// 返回值:0:含有 1:没有
function checkChinese(strTemp) {
var i;
for (i = 0; i < strTemp.length; i++) {
if ((strTemp.charCodeAt(i) < 0) || (strTemp.charCodeAt(i) > 255))
return 0;
}
return 1;
}
// 函数名:checkareaInput
// 功能介绍:检查是否含有非textarea字符
// 参数说明:要检查的字符串
// 返回值:false:含有 true:全部为可textarea字符
function checkareaInput(str) {
var notinput = "<>";
var i;
for (i = 0; notinput != null && i < notinput.length; i++) {
if (str.indexOf(notinput.charAt(i)) >= 0) {// 若有
return false;
}
}
return true;
}
// 函数名:checkInputChr
// 功能介绍:检查是否含有非Input字符
// 参数说明:要检查的字符串
// 返回值:false:含有 true:全部为可Input字符
function checkInputChr(str) {
var pattern = new RegExp("[`~^&*=|'\"<>\\?%#]");
if (pattern.test(str)) {
return false;
}
return true;
}
// 函数名:checkLength
// 功能介绍:检查字符串的长度
// 参数说明:要检查的字符串
// 返回值:长度值
function checkLength(strTemp) {
if (null != strTemp) {
return strTemp.length;
} else {
return 0;
}
}
//函数名:checkHaveBlank
//功能说明:检查字符串是否含有空格
//参数说明:要检查的字符串
//返回值:true:含有,false:不含有
function checkHaveBlank(strTemp) {
if (strTemp.indexOf(" ") >= 0) {
return true;
}
return false;
}
/**
* 校验IP地址是否合法
*
* @param ipValue
* @return
*/
function checkIP(ipValue) {
if (null == ipValue || ipValue.trim() == "") {
return false;
}
var exp = /^(\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])\.(\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])$/;
var reg = ipValue.match(exp);
if (reg == null) {
return false;
} else {
return true;
}
}
/**
* 检查是否为合法的Email Address
* 不能以.或@开头和/或结尾,不能包含1个以上@,形如*@(*.)*
* @param checkStr 要检查的字符串
* @return false:不是 true:是
*/
function chkemail(checkStr) {
var checkOK = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@._-";
var allValid = false;
var checkCode = 0;
if (!checkStr) {
return true;
} else {
for ( var i = 0; i < checkStr.length; i++) {
ch = checkStr.charAt(i);
if (ch == "@") {
if (checkCode == 0 && i > 0) {
checkCode = 1;
} else {
break;
}
}
if (ch == ".") {
if (i == 0 || i == checkStr.length - 1) {
break;
} else if (checkStr.charAt(i + 1) == '.') {
break;
} else if (checkCode == 0) {
if (checkStr.charAt(i + 1) == '@') {
break;
}
} else if (checkCode == 1) {
if (checkStr.charAt(i - 1) == '@') {
break;
} else {
checkCode = 2;
}
}
}
chValid = false;
for ( var j = 0; j < checkOK.length; j++) {
if (ch == checkOK.charAt(j)) {
chValid = true;
break;
}
}
if (!chValid)
break;
if (i == checkStr.length - 1 && checkCode == 2) {
allValid = true;
break;
}
}
return (allValid);
}
}
/**
* 弹出页面
*/
function openwin(url, left, top, width, height, scroll) {
// 如果为0,就取鼠标事件位置
x = left;
if (x == 0)
x = event.screenX + 20;
// 如果为0,就取鼠标事件位置
y = top;
if (y == 0)
y = event.screenY + 20;
var popwin = window.open(url, "newWindow", "height=" + height + ", width="
+ width + ", toolbar =no, menubar=no, scrollbars=" + scroll
+ ", resizable=no, location=no, status=yes, top=" + y + ", left="
+ x + ""); // 写成一行
popwin.focus();
}
/**
* 校验身份证
* 验证通过返回true,否则返回false
*/
function checkeIdCard(idcard) {
var str = idcard;
var Expression = /\d{17}[\d|X]|\d{15}/;
var objExp = new RegExp(Expression);
if (objExp.test(str) == true
&& (idcard.length == 15 || idcard.length == 18)) {
return true;
} else {
return false;
}
}
/**
* 校验mac地址,mac:MAC地址字符串
* 验证通过返回true,否则返回false
*/
function checkMac(mac) {
var pattern = "/^([0-9A-Fa-f]{2})(-[0-9A-Fa-f]{2}){5}|([0-9A-Fa-f]{2})(:[0-9A-Fa-f]{2}){5}/";
eval("var pattern=" + pattern);
var check = pattern.test(mac);
if (check == false) {
return false;
}
return true;
}
/**
* 检验手机号码
* 验证通过返回true,否则返回false
*/
function checkMobile(value) {
if (null != value && value.length != 0) {
if (value.length != 11) {
return false;
}
var i, j, strTemp;
strTemp = "0123456789";
for (i = 0; i < value.length; i++) {
j = strTemp.indexOf(value.charAt(i));
if (j == -1) {
return false;
}
}
return true;
} else {
return false;
}
}
/**
* 检查是否为电话号码
* 参数说明:要检查的字符串
* 返回值:true为是合法,false为不合法
*/
function checkTel(tel) {
var i, j, strTemp;
strTemp = "0123456789--";
for (i = 0; i < tel.length; i++) {
j = strTemp.indexOf(tel.charAt(i));
if (j == -1) {
//说明有字符不合法
return false;
}
}
//说明合法
return true;
}
/**
* 检查是否为大于等于0数字,包含小数、0、正数
* 参数说明:要检查的数字
* 返回值:true为是合法,false为不合法
*/
function checkIsNum(value) {
var i, j, strTemp;
strTemp = "0123456789.";
if (value.length == 0) {
return false;
}
if (value.length > 1 && value.indexOf('0') == 0 && value >= 1) {
return false;
}
var arr = value.split(".");
if (arr.length > 1) {
if (arr[0].length > 1 && arr[0] == 0) {
return false;
}
}
var count = 0;
for (i = 0; i < value.length; i++) {
j = strTemp.indexOf(value.charAt(i));
if (j == -1) {
//说明有字符不是数字
return false;
}
if (value.charAt(i) == '.') {
count++;
}
if (count > 1) {
//说明有字符不是数字
return false;
}
}
//说明是数字
return true;
}
/**
* 检查是否为数字,包含小数、0、正数、负数
* 参数说明:要检查的数字
* 返回值:true为是合法,false为不合法
*/
function checkIsNumOne(value) {
var i, j, strTemp;
strTemp = "-0123456789.";
if (value.length == 0) {
return false;
}
if (value.indexOf('-') != 0) {
return false;
}
if (value == "-0") {
return false;
}
if (value.length > 1 && value.indexOf('0') == 0) {
return false;
}
var count = 0;
var num = 0;
for (i = 0; i < value.length; i++) {
j = strTemp.indexOf(value.charAt(i));
if (j == -1) {
//说明有字符不是数字
return false;
}
if (value.charAt(i) == '.') {
count++;
}
if (count > 1) {
//说明有字符不是数字
return false;
}
if (value.charAt(i) == '-') {
num++;
}
if (num > 1) {
//说明有字符不是数字
return false;
}
}
//说明是数字
return true;
}
/**
* 检查是否为整数,包含0和负数
* 参数说明:要检查的数字
* 返回值:true为是合法,false为不合法
*/
function checkIntNum(value) {
var i, j, strTemp;
strTemp = "-0123456789";
if (value.length == 0) {
return false;
}
if (value.indexOf('-') != 0) {
return false;
}
if (value == "-0") {
return false;
}
if (value.length > 1 && value.indexOf('0') == 0) {
return false;
}
var count = 0;
for (i = 0; i < value.length; i++) {
j = strTemp.indexOf(value.charAt(i));
if (j == -1) {
//说明有字符不是整数
return false;
}
if (value.charAt(i) == '-') {
count++;
}
if (count > 1) {
//说明有字符不是数字
return false;
}
}
//说明是整数
return true;
}
/**
* 检查是否为正整数,可以为0
* 参数说明:要检查的数字
* 返回值:true为是合法,false为不合法
*/
function checkPositiveNum(value) {
var i, j, strTemp;
strTemp = "0123456789";
if (value.length == 0) {
return false;
}
if (value.length > 1 && value.indexOf('0') == 0) {
return false;
}
for (i = 0; i < value.length; i++) {
j = strTemp.indexOf(value.charAt(i));
if (j == -1) {
//说明有字符不是正整数
return false;
}
}
//说明是正整数
return true;
}
/**
* 是否符合httpurl格式
* @param str_url
* @returns {Boolean}
*/
function isHttpURL(str_url) {
var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
+ "|" // 允许IP和DOMAIN(域名)
+ "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
+ "[a-z]{2,6})" // first level domain- .com or .museum
+ "(:[0-9]{1,4})?" // 端口- :80
+ "((/?)|" // a slash isn't required if there is no file name
+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
var re = new RegExp(strRegex);
if (re.test(str_url)) {
return true;
} else {
return false;
}
}
/**
* 检查当前浏览器类型,返回浏览器名
* @return 浏览器名称
*/
function checkBrowser() {
var name = "Unknown";
if (window.ActiveXObject) {
name = "IE";
} else if (navigator.userAgent.toLowerCase().indexOf("firefox") != -1) {
name = "Firefox";
} else if ((typeof document.implementation != "undefined")
&& (typeof document.implementation.createDocument != "undefined")
&& (typeof HTMLDocument != "undefined")) {
name = "Mozilla";
} else if (navigator.userAgent.toLowerCase().indexOf("opera") != -1) {
name = "Opera";
}
return name;
}