[javascript]代码库
// 阻止网页上的右键操作
function Click() {
window.event.returnValue = false;
}
//document.oncontextmenu = Click; // 阻止网页右键的脚本
// 阻止F5刷新键
function OverKey(){
if(116==event.keyCode){
event.keyCode=0;
event.returnValue = false;
}
}
document.onkeydown = OverKey; // 阻止F5刷新键的脚本
document.onclick = OverKey; // 阻止F5刷新键的脚本
// 用正则表达式将前后空格替换成空字符串
String.prototype.trim = function trim() {
return this.replace(/(^\s*)|(\s*$)/g,"");
};
function removeSpace(obj) {
if(obj!=null)
obj.value = obj.value.trim();
return;
}
// 控制页面重复提交
// mainForm:页面当前提交form;flag:true-不可操作、false-可操作
function repeatSubmitControl(mainForm,flag){
if(true==flag){
for(var i=0;i<mainForm.length;i++){
var tmpElement = mainForm.elements[i];
if(undefined==tmpElement.type)
continue;
if("submit"==tmpElement.type.toLowerCase() || "button"==tmpElement.type.toLowerCase())
tmpElement.disabled = true;
}
}
else if(false==flag){
for(var i=0;i<mainForm.length;i++){
var tmpElement = mainForm.elements[i];
if(undefined==tmpElement.type)
continue;
if("submit"==tmpElement.type.toLowerCase() || "button"==tmpElement.type.toLowerCase())
tmpElement.disabled = false;
}
}
}
// 提交交易请求
// mainForm:页面当前提交form;actionName:访问交易名称
function submitTrs(mainForm,actionName) {
repeatSubmitControl(mainForm,true);
document.getElementById("errormsg").innerHTML = "交易正在处理中...";
mainForm.action = actionName;
mainForm.submit();
repeatSubmitControl(mainForm,false);
document.getElementById("errormsg").innerHTML = "";
}
// 检查是否为空
function checkIsEmpty(obj,message,areaname) {
var val = obj.value.trim();
if(val==null || val==''){
document.getElementById(areaname).innerHTML = message;
return true;
}
return false;
}
// 检查长度
function checkLength(obj,minlength,maxlength,message,areaname) {
var val = obj.value.trim();
if(val.length<minlength || val.length>maxlength){
document.getElementById(areaname).innerHTML = message;
return false;
}
return true;
}
// 控制只能输入英文、数字
function checkIsNumberOrLetter(obj) {
obj.value = obj.value.replace(/[^a-zA-Z0-9]/g,'');
return false;
}
// 控制只能输入数字
function checkIsNumber(obj) {
obj.value = obj.value.replace(/[^0-9]/g,'');
return false;
}
// 检查身份证格式
function checkIdNoStyle(obj,message,areaname) {
var idNo = obj.value.trim();
var pattern = /^(([0-9]{15})|([0-9]{17}[a-zA-Z0-9]{1}))$/;
if(!pattern.exec(idNo)){
document.getElementById(areaname).innerHTML = message;
return false;
}
return true;
}
// 检查电子邮件地址格式
function checkEmailStyle(obj,message,areaname) {
var email = obj.value.trim();
var pattern = /^([a-zA-Z0-9_\-]{1,}@[a-zA-Z0-9_\-]{1,}\.[a-zA-Z0-9_\-.]{1,})$/;
if(!pattern.exec(email)){
document.getElementById(areaname).innerHTML = message;
return false;
}
return true;
}
// 检查固定电话格式
function checkPhoneStyle(obj,message,areaname) {
var phone = obj.value.trim();
var pattern = /^((0[0-9]{2,4}\-)?([1-9][0-9]{5,7})+(\-[0-9]{1,4})?)$/;
if(!pattern.exec(phone)){
document.getElementById(areaname).innerHTML = message;
return false;
}
return true;
}
// 输入金额的显示格式化
function standMoney(inObject){
var inStr = inObject.value;
var outStr = "";
var charValue = "";
for(var i=0;i<inStr.length;i++){
charValue = inStr.charAt(i);
if(!(isNaN(parseInt(charValue,10))) || (charValue=='.') || (charValue==','))
outStr += charValue;
}
var valueArr = outStr.split(".");
if(valueArr.length>2){
outStr = valueArr[0] + "." + valueArr[1];
valueArr = outStr.split(".");
}
var dotValue;
var dotStr = ".";
if(valueArr.length==2){
dotValue = valueArr[1];
if(dotValue.length==0)
dotStr = dotStr + "";
else if(dotValue.length==1)
dotStr = dotStr + dotValue;
else
dotStr = dotStr + dotValue.substring(0,2);
}
else
dotStr = "";
var intArr = valueArr[0].split(",");
var intValue = "";
for(i=0;i<intArr.length;i++){
intValue += intArr[i];
}
var intStr = "";
while(intValue.length>3){
intStr = "," + intValue.substring(intValue.length-3,intValue.length) + intStr;
intValue = intValue.substring(0,intValue.length-3);
}
intStr = intValue + intStr;
outStr = intStr + dotStr;
inObject.value = outStr;
return;
}
// 输入的金额提交格式化
function lastMoney(inObject) {
var inStr = inObject.value;
var outStr;
var valueArr;
valueArr= inStr.split(".");
var dotStr,dotValue;
if(valueArr.length==2){
dotValue = valueArr[1];
if(dotValue.length==0){
dotStr = "00";
}
else {
if(dotValue.length==1)
dotStr = dotValue + "0";
else
dotStr = dotValue.substring(0,2);
}
}
else{
dotStr = "00";
}
var intArr;
intArr = valueArr[0].split(",");
var intValue;
intValue = "";
for(var i=0;i<intArr.length;i++) {
intValue+=intArr[i];
}
intStr = intValue;
if( intStr == "" || intStr == null)
intStr = "0";
outStr = intStr + "." + dotStr;
if( outStr == "" || outStr == null || outStr == ".00" || outStr == "0.00" || outStr == ".")
inObject.value = "";
else
inObject.value = outStr;
return true;
}