首页 > 代码库 > JAVASCRIPT校验大全[转]
JAVASCRIPT校验大全[转]
var IsFireFox = document.getElementById &&! document.all;//判断是否为FireFox
//页面里回车到下一控件的焦点
function Enter2Tab(e)
{
try
{
var ob = IsFireFox ? e.target : event.srcElement;
if(ob.tagName == "INPUT" &&
(
ob.type == "text" ||
ob.type == "password" ||
ob.type == "checkbox" ||
ob.type == "radio"
) ||
ob.tagName == "SELECT")
{
var key = IsFireFox ? e.which : event.keyCode;
if (key == 13)
{
if (IsFireFox)
{
event.which = 9;
}
else
{
event.keyCode = 9;
}
}
}
}
catch(E){}
}
//打开此功能请取消下行注释
//document.onkeydown = Enter2Tab;
//ajax对象创建
function AjaxLib()
{
var objXMLHttp;
if (window.XMLHttpRequest)
{
objXMLHttp = new XMLHttpRequest();
}
else
{
//使用该代码,出现问题 var MSXML = [’MSXML2.XMLHTTP.5.0’, ’MSXML2.XMLHTTP.4.0’, ’MSXML2.XMLHTTP.3.0’, ’MSXML2.XMLHTTP’, ’Microsoft.XMLHTTP’];
var MSXML = [’MSXML2.XMLHTTP’, ’Microsoft.XMLHTTP’];
for(var n = 0; n < MSXML.length; n ++)
{
try
{
objXMLHttp = new ActiveXObject(MSXML[n]);
break;
}
catch(e)
{
}
}
if (!objXMLHttp && typeof XMLHttpRequest != ’undefined’)
{
objXMLHttp = new XMLHttpRequest();
}
}
return objXMLHttp;
}
//判断用户名是否符合标准
function UserNameIsOk(ssn)
{
var re = /^[0-9a-z][w-.]*[0-9a-z]$/i;
return re.test(ssn) ? true : false;
}
//_______全选择__________
function SelectAll()
{
var empty;
var f = document.forms[0];
for (var i = 0; i < f.length; i++)
{
empty = f[i];
if (empty.type == "checkbox" && empty.disabled == false)
empty.checked = true;
}
}
//__________返选择_________
function SelectReverse()
{
var empty;
var f = document.forms[0];
for (var i = 0; i < f.length; i++)
{
empty = f[i];
if (empty.type == "checkbox" && empty.disabled == false)
if(empty.checked == true)
{
empty.checked = false;
}
else
{
empty.checked = true;
}
}
}
//提交查询内容 submit search find content
function GoPage(pn)
{
try
{
document.forms[0].page.value = pn;
document.forms[0].submit();
}
catch(E)
{
document.forms[1].page.value = pn;
document.forms[1].submit();
}
}
function WoodsAlert(ob, tip)
{
alert(tip);
//ob.value = "";
ob.focus();
}
//__________处理特殊字符________________
function invalid_keyword()
{
var keyword = new Array("<", ">", "%", "’", """);
var f, ob;
for (var form = 0; form < document.forms.length; form++)
{
f = document.forms[form];
for (var i = 0; i < f.length; i++)
{
ob = f[i];
if (ob.type == "text" && ob.disabled == false)
{
for (j = 0; j < keyword.length; j++)
{
if (ob.value.indexOf(keyword[j]) != -1)
{
alert("在输入框聚焦处发现非法字符[" + keyword[j] + "]。");
//ob.value = "";
ob.focus();
return false;
}
}
ob.value = Trim(ob.value);
}
}
}
return true;
}
function IsTime(str)
//__________判断是否为时间字符串________________________
{
var a = str.match(/^(d{1,2})(:)?(d{1,2})2(d{1,2})$/);
if (a == null) {alert(’输入的参数不是时间格式’); return false;}
if (a[1]>24 || a[3]>60 || a[4]>60)
{
return false
}
return true;
}
function IsDate(str)
//_________是否为日期(YYYY-MM-DD)类型字符串_________________________
{
var r = str.match(/^(d{1,4})(-|/)(d{1,2})2(d{1,2})$/);
if (r == null) return false;
var d = new Date(r[1], r[3]-1, r[4]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}
function IsBigCurrentDate(str)
//_________________判断用户给出的日期是否大于当前日期______________________________
{
var today = new Date();
var tY = String(today.getFullYear());
var tM = String(today.getMonth() + 1);
(tM.length <= 1) ? tM = "0" + tM : tM = tM;
var tD = String(today.getDate());
(tD.length <= 1) ? tD = "0" + tD : tD = tD;
var tH = String(today.getHours());
(tH.length <= 1) ? tH = "0" + tH : tH = tH;
var tM1 = String(today.getMinutes());
(tM1.length <= 1) ? tM1 = "0" + tM1 : tM1 = tM1;
var tdate = tY + "-" + tM + "-" + tD + " " + tH + ":" + tM1 + ":59";//zhuweisong 添加秒的叛断
if (str > tdate)
{
return true;
}
else
{
return false;
}
}
function IsDateTime(str)
//____________是否为时间日期类型__________________________________
{
var reg = /^(d{1,4})(-|/)(d{1,2})2(d{1,2}) (d{1,2}):(d{1,2}):(d{1,2})$/;
var r = str.match(reg);
if (r == null) return false;
var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);
}
function IsEmail(mail)
//_____________判断是否是有效的EMAIL地址________________________________
{
return(new RegExp(/^w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+)*.[A-Za-z0-9]+$/).test(mail));
}
function IsIp(s)
//________________判断是否为有效IP地址_________________________________
{
var check = function(v)
{
try
{
return (v <= 255 && v >= 0);
}
catch(x)
{
return false;
}
}
var re = s.split(".")
return (re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3])):false
}
//_________判断输入的是否为汉字___________begin__________
function isCharsInBag (s, bag)
{
var i,c;
for (i = 0; i < s.length; i++)
{
c = s.charAt(i);//字符串s中的字符
if (bag.indexOf(c) > -1)
return c;
}
return "";
}
function IsCn(s)
{
var errorChar;
var badChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789><,[]{}?/+=|’":;~!#$%()`";
errorChar = isCharsInBag(s, badChar)
if (errorChar != "" )
{
return false;
}
return true;
}
//_________判断输入的是否为汉字___________end__________
//________函数功能:判断输入的字符串是不是英文__________begin___________
function isCharsInBagEn (s, bag)