说明:本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)||(s.length>Max))
??????? return false;
??? else
??????? return true;
}
?
//15位以内字母,数字,下划线
function hasAccountChar(s){
??? var p = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,15}$/;
??? return p.test(s);
}
?
//判断ie6
function isIE6(){
?return ($.browser.msie&&$.browser.version=='6.0');
}
?
//jquery去除全选操作后,全选那勾的去除
function cancelCheckbox(){
?$('input[type=checkbox]').eq(0).attr('checked',false);
}
?
//只能输入数字和小数点
function clearNoNum(obj) {
??//先把非数字的都替换掉,除了数字和.
?obj.value = obj.value.replace(/[^\d.]/g, "");
??//必须保证第一个为数字而不是.
?obj.value = obj.value.replace(/^\./g, "");
??//保证只有出现一个.而没有多个.
?obj.value = obj.value.replace(/\.{2,}/g, ".");
??//保证.只出现一次,而不能出现两次以上
?obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
}
?
//进行Iframe的自动撑开,让所有父页面的Iframe都自动适应包含页高度??
function iframeAutoHeight() {
?var doc = document, p = window;
?while (p = p.parent) {
??var frames = p.frames, frame, i = 0;
??while (frame = frames[i++]) {
???if (frame.document == doc) {??
??????????? //??? frame.frameElement.style.height = doc.body.scrollHeight;?
????frame.frameElement.style.height = 200;
????doc = p.document;
????break;
???}
??}
??if (p == top) {
???break;
??}
?}
}
?
//获得URL路径中的参数值
function getUrlPara(paras) {
?var url = location.href;
?var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
?var paraObj = {};
?for (i = 0; j = paraString[i]; i++) {
??paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
?}
?var returnValue = paraObj[paras.toLowerCase()];
?if (typeof (returnValue) == "undefined") {
??return "";
?} else {
??return returnValue;
?}
}
?
//只能输入数字
/***
*?不带负号的输入
*?调用方式:onkeydown = "DigitInput(this,event);"
*?8:退格键、46:delete、37-40: 方向键
*?48-57:小键盘区的数字、96-105:主键盘区的数字
*?110、190:小键盘区和主键盘区的小数
*?189、109:小键盘区和主键盘区的负号
***/
function DigitInputOnly(el,ev) {
??? var event = ev || window.event;???????????????????????????? //IE、FF下获取事件对象
??? var currentKey = event.charCode||event.keyCode;???????????? //IE、FF下获取键盘码
???
??? //小数点处理
??? if (currentKey == 110 || currentKey == 190) {
??????? if (el.value.indexOf(".")>=0)
??????????? if (window.event)?????????????????????? //IE
??????????????? event.returnValue=false;???????????????? //e.returnValue = false;效果相同.
??????????? else??????????????????????????????????? //Firefox
??????????????? event.preventDefault();
??? } else
??????? if (currentKey!=8 && currentKey != 46 && (currentKey<37 || currentKey>40) && (currentKey<48 || currentKey>57) && (currentKey<96 || currentKey>105))
??????????? if (window.event)?????????????????????? //IE
??????????????? event.returnValue=false;???????????????? //e.returnValue = false;效果相同.
??????????? else??????????????????????????????????? //Firefox
??????????????? event.preventDefault();
}
?
/***
?带负数的输入
*?调用方式:onkeydown = "DigitInput(this,event);"
?8:退格键、46:delete、37-40: 方向键
?48-57:小键盘区的数字、96-105:主键盘区的数字
?110、190:小键盘区和主键盘区的小数
?189、109:小键盘区和主键盘区的负号
***/
function DigitInputWith(el,ev) {
??? var event = ev || window.event;???????????????????????????? //IE、FF下获取事件对象
??? var currentKey = event.charCode||event.keyCode;???????????? //IE、FF下获取键盘码
???
??? //小数点处理
??? if (currentKey == 110 || currentKey == 190) {
??????? if (el.value.indexOf(".")>=0)
??????????? if (window.event)?????????????????????? //IE
??????????????? event.returnValue=false;???????????????? //e.returnValue = false;效果相同.
??????????? else??????????????????????????????????? //Firefox
??????????????? event.preventDefault();
??? } else
??????? //负号处理
??????? if (currentKey == 189 || currentKey == 109) {
??????????? if (getPosition(el)>0 || el.value.indexOf("-")>=0)
??????????????? if (window.event)?????????????????????? //IE
??????????????????? event.returnValue=false;???????????????? //e.returnValue = false;效果相同.
??????????????? else??????????????????????????????????? //Firefox
??????????????????? event.preventDefault();
??????? } else
??????? if (currentKey!=8 && currentKey != 46 && (currentKey<37 || currentKey>40) && (currentKey<48 || currentKey>57) && (currentKey<96 || currentKey>105))
??????????? if (window.event)?????????????????????? //IE
??????????????? event.returnValue=false;???????????????? //e.returnValue = false;效果相同.
??????????? else??????????????????????????????????? //Firefox
??????????????? event.preventDefault();
}
?
/**
? * 获取光标所在的字符位置
? * @param obj 要处理的控件, 支持文本域和输入框
? * @author hotleave
? */
function getPosition(obj){
??? var result = 0;
??? if(obj.selectionStart){ //非IE浏览器
??????? result = obj.selectionStart
??? }else{????????????????? //IE
??????? var rng;
??????? if(obj.tagName == "TEXTAREA"){ //如果是文本域
??????????? rng = event.srcElement.createTextRange();
??????????? rng.moveToPoint(event.x,event.y);
??????? }else{???????????????????????? //输入框
??????????? rng = document.selection.createRange();
??????? }
??????? rng.moveStart("character",-event.srcElement.value.length);
??????? result = rng.text.length;
??? }
??? return result;
}
?
//只能输入数字和字母
function checktest(obj){
?var?? re=/^[A-Za-z0-9]*$/;
?var?? str= '';
?if?? (re.test(obj.value)==false){
??obj.value=str;
?}else{
??str=obj.value;
?}
}
?