日期:2014-05-16  浏览次数:20334 次

javascript常用验证 常用操作方法(工具方法)

说明:本js文件中包括了一些常用的js验证和常用的js操作方法(源码见附件)

?

//获取字符串真实长度,一个汉字为两个长度,一个英文字符或数字为一个字符

function getTrueLenth(str){

?? ?return str.replace(/[^\x00-\xff]/g,"xx").length;

}

?

?

//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”
function isTel(object){
//国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"
?var s =document.getElementById(object.id).value;
?var pattern =/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
?//var pattern =/(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/;
???? if(s!="")
???? {
???????? if(!pattern.exec(s))
???????? {
????????? alert('请输入正确的电话号码:电话号码格式为国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"');
????????? object.value="";
????????? object.focus();
???????? }
???? }
}

?

//邮箱验证
function Check(object){
? var s =document.getElementById(object.id).value;
?????? var pattern =/^[a-zA-Z0-9_\-]{1,}@[a-zA-Z0-9_\-]{1,}\.[a-zA-Z0-9_\-.]{1,}$/;
?????????? if(s!="")
?????????? {
?????????????? if(!pattern.exec(s))
?????????????? {
??????????????? alert('请输入正确的邮箱地址');
??????????????? object.value="";
??????????????? object.focus();
?????????????? }
?????????? }
?????????
? }

?

//验证电子邮箱地址
function verifyEmailAddress(email){?
  var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;?
  flag = pattern.test(email);?
  if(flag){?
    return true;?
  }else{?
    return false;?
  }?
}?
String.prototype.Trim = function() {
?return this.replace(/(^\s*)|(\s*$)/g, "");
}?
String.prototype.LTrim = function() {
?return this.replace(/(^\s*)/g, "");
}?
String.prototype.RTrim = function() {
?return this.replace(/(\s*$)/g, "");
}

?

?

//字符处理;
//去左右空格;
function trim(s){
???? return rtrim(ltrim(s));
}


//去左空格;
function ltrim(s){
???? return s.replace( /^\s*/, "");
}


//去右空格;
function rtrim(s){
???? return s.replace( /\s*$/, "");
}

?

//过滤HTML字符
function HTML(text){
??? text = text.replace(/&/g, "&");
//??? text = text.replace(/"/g, """);
??? text = text.replace(/</g, "<");
??? text = text.replace(/>/g, ">");
??? text = text.replace(/'/g, "’");
??? return text ;
}

?

//还原HTML字符
function ReHTML(text){
??? text = text.replace(/&/g, "&");
//??? text = text.replace(/"/g, '"');
//??? text = text.replace(/</g, "<");
??? text = text.replace(/>/g, ">");
??? text = text.replace(/’/g, "'");
??? return text ;
}

?

// 判断中英文混排时候的长度
function byteLength (sStr) {
??? aMatch = sStr.match(/[^\x00-\x80]/g);
??? return (sStr.length + (! aMatch ? 0 : aMatch.length));
}

?

//获取字符串中英文长度英文返回1其他都是返回2(以每个字符做单位) tmp是总长度
?//参数o作为字符串
?function getStrLength(o){
??var tmp = 0;?
??var txReg = /[u4E00-u9FA5uF900-uFA2D]/;
??if(null != o && '' != o){
???for(var i = 0; i < o.length; i++){
????tmp += txReg.test(o.charAt(i))? ? 1 : 2;
???}
??}
??return tmp;
?}

?

//空字符值;
function isEmpty(s){
??? s = trim(s);
??? return s.length == 0;
}

?

//数字;
function isNumber(s){
??? return !isNaN(s);
}

?

//身份证;
function isCard(s){
??? s = trim(s);
??? var p = /^\d{15}(\d{2}[xX0-9])?$/;
??? return p.test(s);
}

?

//URL;
function isURL(s){
??? s = trim(s).toLowerCase();
??? var p = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
??? return p.test(s);
}

?

//限定长度
function limitLen(s,Min,Max){
??? s=trim(s);
??? if(s=="") return false;
??? if((s.length<Min)|