CommonFn.js
2017-07-10 09:53:32 访问(1742) 赞(0) 踩(0)
JS闭合封装一些常用的方法
(function () {
var CF = {};
window["CF"] = CF;
// 字符串相关的方法 //
var StrFn = {
Trim: function (str) {
// Trim + //
if (str == null)
return "";
return str.replace(/(^\s*)|(\s*$)/g, "");
},
LeftTrim: function (str) {
// 左边Trim + //
if (str == null)
return "";
return str.replace(/(\s*)(.*)/g, "$2");
},
Reverse: function (str) {
// 反转+ //
if (str == null)
return "";
var theResult = "";
var theLen = str.length;
for (var i = theLen - 1; i >= 0; --i) {
theResult = theResult + str.substr(i, 1);
}
return theResult;
},
RightTrim: function (str) {
// 右边Trim+ //
if (str == null)
return "";
str = Reverse(str);
str = LeftTrim(str);
str = Reverse(str);
return str;
},
IsBlank: function (str) {
// 是否为空+ //
if (str == null)
return true;
str = str + "";
str = Trim(str);
if (str == "")
return true;
return false;
},
GetLowerCase: function (str) {
// 转小写+ //
if (str == null)
return true;
return str.toLowerCase();
},
GetUpperCase: function (str) {
// 转大写+ //
if (str == null)
return true;
return str.toUpperCase();
},
StartsWith: function (str, theChar) {
// 字符串开始于+ //
if (str == null || str == "" || theChar == null || theChar == "")
return false;
var idx = str.indexOf(theChar);
if (idx == 0)
return true;
else
return false;
},
EndsWith: function (str, theChar) {
// 字符串结束于+ //
if (str == null || str == "" || theChar == null || theChar == "")
return false;
var idx = str.lastIndexOf(theChar);
var strLen = str.length;
var theCharLen = theChar.length;
if (idx == strLen - theCharLen)
return true;
else
return false;
},
NotStartsWith: function (str, theChar) {
// 字符串不开始于+ //
if (str == null || str == "" || theChar == null || theChar == "")
return true;
var idx = str.indexOf(theChar);
if (idx == 0)
return false;
else
return true;
},
SubString: function (str, startIndex) {
if (str == null || str == "")
return "";
if (startIndex == 0)
return str;
var theLen = str.length;
if (startIndex >= theLen)
return str;
return str.substr(startIndex, theLen);
}
};
window["CF"]["StrFn"] = StrFn;
// 数字运算相关的方法 //
var MathFn = {
ToDefaultInt: function (str) {
var theStr = StrFn.Trim(str);
if (theStr == "")
return 0;
return parseInt(theStr, 10);
},
ToInt: function (str, defaultValue) {
var theStr = StrFn.Trim(str);
if (theStr == "")
return defaultValue;
return parseInt(theStr, 10);
}
};
window["CF"]["MathFn"] = MathFn;
// 数组操作相关的方法 //
var ArrayFn = {
InsertItem: function (theArray, theItem, idx) {
if (theArray == null)
theArray = new Array();
if (theItem == null)
return theArray;
var theLen = theArray.length;
if (idx >= theLen || idx < 0)
theArray[theLen] = theItem;
else {
for (var i = theLen; i > idx; --i) {
theArray[i] = theArray[i - 1];
}
theArray[idx] = theItem;
}
return theArray;
},
RemoveItem: function (theArray, theItem) {
if (theArray == null)
return false;
if (theItem == null) {
return false;
}
var theLen = theArray.length;
if (theLen == 0) {
return false;
}
var i = 0;
for (i = 0; i < theLen; ++i) {
// if (theArray[i].ID == theObj.ID)
if (theArray[i] == theItem)
break;
}
if (i == theLen) {
// alert("3");
return false;
}
while (i < (theLen - 1)) {
theArray[i] = theArray[i + 1];
++i;
}
theArray.pop();
return true;
},
MoveItem: function (theArray, theItem, idx) {
if (theArray == null || theItem == null)
return false;
RemoveItem(theArray, theItem);
InsertItem(theArray, theItem, idx);
},
Merge: function (theArray, mergeArray) {
if (theArray == null)
theArray = new Array();
if (mergeArray == null)
return theArray;
var mergeLen = mergeArray.length;
if (mergeLen == 0)
return theArray;
var theLen = theArray.length;
var i = 0;
for (i = 0; i < mergeLen; ++i) {
theArray[theLen + i] = mergeArray[i];
}
for (i = 0 ; i < mergeLen; ++i) {
mergeArray.pop();
}
return theArray;
}
};
window["CF"]["ArrayFn"] = ArrayFn;
// 控件相关的方法 //
var CtrlFn = {
GetCtrl: function (ctrlId) {
return document.getElementById(ctrlId);
},
SetAttribute: function (vCtrl, AttributeName, AttributeValue) {
if (vCtrl == null)
return false;
if (StrFn.IsBlank(AttributeName))
return false;
vCtrl.setAttribute(AttributeName, AttributeValue);
return true;
},
SetDisabledByCtrlId: function (ctrlId) {
var theCtrl = GetCtrl(ctrlId);
return SetDisabled(theCtrl);
},
SetDisabled: function (ctrl) {
if (ctrl == null)
return false;
return SetAttribute(ctrl, "disabled", "disabled");
},
SetFocusAndSelectByCtrlId: function (ctrlId) {
var ctrl = document.getElementById(ctrlId);
if (ctrl == null)
return false;
ctrl.focus();
ctrl.select();
return true;
},
SetFocusByCtrlId: function (ctrlId) {
var ctrl = document.getElementById(ctrlId);
if (ctrl == null)
return false;
ctrl.focus();
return true;
},
AlertAndSetFocusAndSelectByCtrlId: function (alertMsg, ctrlId) {
alert(alertMsg);
var ctrl = document.getElementById(ctrlId);
if (ctrl == null)
return false;
ctrl.focus();
ctrl.select();
return true;
},
AlertAndSetFocusByCtrlId: function (alertMsg, ctrlId) {
alert(alertMsg);
var ctrl = document.getElementById(ctrlId);
if (ctrl == null)
return false;
ctrl.focus();
return true;
},
};
window["CF"]["CtrlFn"] = CtrlFn;
// CheckBox 控件相关的方法 //
var CheckBoxFn = {
IsCheck: function (ctrl) {
if (ctrl == null)
return false;
return ctrl.checked;
},
IsCheckByCtrlId: function (ctrlId) {
var ctrl = document.getElementById(ctrlId);
if (ctrl == null)
return false;
return ctrl.checked;
},
SetCheck: function (ctrl, bFlag) {
if (ctrl == null)
return false;
ctrl.checked = bFlag;
return true;
}
};
window["CF"]["CheckBoxFn"] = CheckBoxFn;
// CheckBox 控件相关的方法 //
var ValidFn = {
TextBoxIsEmptyByCtrlId: function (ctrlId) {
var ctrl = document.getElementById(ctrlId);
if (ctrl == null)
return true;
var theValue = StrFn.Trim(ctrl.value);
if (theValue == "")
return true;
else
return false;
},
DropDownListIsEmptyByCtrlId: function (ctrlId) {
var ctrl = document.getElementById(ctrlId);
if (ctrl == null)
return true;
var theValue = StrFn.Trim(ctrl.value);
if (theValue == "")
return true;
else
return false;
},
CheckTextBox: function (bFlag, ctrlId, LabelName) {
// 关闭校验 //
if (bFlag == 0)
return true;
var theCtrl = document.getElementById(ctrlId);
if (theCtrl == null) {
alert("没有获得控件" + ctrlId);
return false;
}
if (StrFn.Trim(theCtrl.value) == "") {
alert("请输入" + LabelName);
theCtrl.focus();
theCtrl.select();
return false;
}
return true;
},
CheckOper: function (bFlag, ctrlId, LabelName, checkType) {
// 关闭校验 //
if (bFlag == 0)
return true;
var theCtrl = document.getElementById(ctrlId);
if (theCtrl == null) {
alert("没有获得控件" + ctrlId);
return false;
}
var theValue = StrFn.Trim(theCtrl.value);
if (theValue == "") {
if (bFlag == 1) {
alert("请输入" + LabelName);
theCtrl.focus();
theCtrl.select();
return false;
}
else {
return true;
}
}
var checkResult = true;
switch (checkType) {
case "zip":
checkResult = ValidFn.IsValidZip(theValue);
break;
case "tel":
checkResult = ValidFn.IsValidTel(theValue);
break;
case "email":
checkResult = ValidFn.IsValidEmail(theValue);
break;
case "mobile":
checkResult = ValidFn.IsValidMobile(theValue);
break;
case "qq":
checkResult = ValidFn.IsValidQQ(theValue);
break;
}
if (!checkResult) {
alert("请输入合理的" + LabelName);
theCtrl.focus();
theCtrl.select();
return false;
}
return true;
},
CheckZip: function (bFlag, ctrlId, LabelName) {
return ValidFn.CheckOper(bFlag, ctrlId, LabelName, "zip");
},
CheckTel: function (bFlag, ctrlId, LabelName) {
return ValidFn.CheckOper(bFlag, ctrlId, LabelName, "tel");
},
CheckEmail: function (bFlag, ctrlId, LabelName) {
return ValidFn.CheckOper(bFlag, ctrlId, LabelName, "email");
},
CheckMobile: function (bFlag, ctrlId, LabelName) {
return ValidFn.CheckOper(bFlag, ctrlId, LabelName, "mobile");
},
CheckQQ: function (bFlag, ctrlId, LabelName) {
return ValidFn.CheckOper(bFlag, ctrlId, LabelName, "qq");
},
IsValidZip: function (zip) {
if (zip == null)
return false;
var reg = /[^0-9]/g;
if (reg.test(zip))
return false;
return zip.length == 6;
},
IsValidTel: function (theValue) {
if (theValue == null)
return false;
var reg = /^[0-9]{3,4}(\-)?\d{7,8}(\(\d{1,6}\))?$/;
return reg.test(theValue);
},
IsValidEmail: function (theValue) {
if (theValue == null)
return false;
var reg = /^[_a-zA-Z0-9+.]+@([_a-zA-Z0-9]+.)+[a-zA-Z0-9]{2,3}$/;
return reg.test(theValue);
},
IsValidMobile: function (theValue) {
if (theValue == null)
return false;
var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
return reg.test(theValue);
},
IsValidQQ: function (theValue) {
if (theValue == null)
return false;
var reg = /^[1-9][0-9]{4,}$/;
return reg.test(theValue);
},
IsAllDigitChar: function (str) {
if (str == null)
return false;
var reg = /[^0-9]/g;
return (!reg.test(str));
},
IsFloat: function (str) {
if (str == "0")
return true;
// 是否浮点数 //
var bFlag = (str.match(/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/) == null);
if (!bFlag)
return true;
bFlag = (str.match(/^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/) == null);
if (!bFlag)
return true;
return false;
},
IsFloatGreaterEqualZero: function (str) {
if (str == "0")
return true;
// 是否浮点数 //
var bFlag = (str.match(/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/) == null);
if (!bFlag)
return true;
return false;
},
IsFloatLessZero: function (str) {
// 是否浮点数 //
var bFlag = (str.match(/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/) == null);
if (!bFlag)
return true;
return false;
}
};
window["CF"]["ValidFn"] = ValidFn;
/************End******************/
})();
上一条:
下一条:
相关评论
发表评论