﻿/*Demo:
*验证两个汉字以上写法:show(id,value.isTwoChinese())
*或者为空,不为空则必须两个汉字以上写法:show(id,value.isNull()||value.isTwoChinese())
*文本域长度限制写法:maxTxt(id,'150')
*可为空文本写法:showPss(id)*/
var isValid = true;
String.prototype.regex = function (regexString) { return regexString.test(this); }
String.prototype.isOk = function () { return true; }
String.prototype.isPass = function (flag) { return this == "" ? true : flag; }
//是否为空或者全部都是空格
String.prototype.isNull = function () { return this.regex(/^\s*$/); }
//是否为空或者全部都是空格
String.prototype.isNotNull = function () { return !this.isNull(); }
//是否为空####
String.prototype.isIndex0 = function () { return this == "###" ? false : true; }
//是否为IP
String.prototype.isIP = function () { return this.regex(/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/); }
//是否为邮箱
String.prototype.isEmail = function () { return this.regex(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/); }
//是否符合整数格式
String.prototype.isInteger = function () { return this.regex(/^[-]?\d+$/); }
//是否为正整数
String.prototype.isNumber = function () { return this.regex(/^\d+$/); }
//视力验证
String.prototype.isVision = function () { return this.regex(/^[1-5].\d$/); }
//是否是带小数的数字格式,可以是负数
String.prototype.isDecimal = function () { return this.regex(/^[-]?\d+(.\d+)?$/); }
//是否符合金额格式(格式定义为带小数的正数，小数点后最多2位 )
String.prototype.isMoney = function () { return this.regex(/^\d{1,8}(,\d{3})*(\.\d{1,2})?$/); }
//是否符合手机号格式
String.prototype.isMobile = function () { return this.regex(/^1(3|5|8)\d{9}$/); }
//是否为端口格式
String.prototype.isPort = function () { return (this.isNumber() && this < 65536); }
//是否只由英文字母和数字和下划线组成
String.prototype.isNumberOr_Letter = function () { return this.regex(/^[0-9a-zA-Z\_]+$/); }
//是否只由英文字母和数字组成
String.prototype.isNumberOrLetter = function () { return this.regex(/^[0-9a-zA-Z]+$/); }
//是否只由汉字、字母、数字组成
String.prototype.isChinaOrNumbOrLett = function () { return this.regex(/^[0-9a-zA-Z\u4e00-\u9fa5]+$/); }
//是否只由汉字、字母组成
String.prototype.isChinaOrLett = function () { return this.regex(/^[a-zA-Z\u4e00-\u9fa5]+$/); }
//是否电话号码格式
String.prototype.isPhone = function () { return this.regex(/^(0\d{2,3}-)?\d{7,8}(-\d{1,4})?$/); }
//是否传真号码格式
String.prototype.isFax = function () { return this.regex(/^(86\-)?(\d{2,4}-)?([2-9]\d{6,7})+(-\d{1,4})?$/); }
//是否电话号码或手机格式
String.prototype.isPhoneOrMobile = function () { return (this.isPhone() || this.isMobile()); }
//是否存在两个以上汉字
String.prototype.isTwoChinese = function () { return this.regex(/[\u4e00-\u9fa5]+.*[\u4e00-\u9fa5]/); }
//是否为网址格式
String.prototype.isWebUrl = function () { return this.regex(/^(http|https|ftp):\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$/); }
//是否为身份证格式|身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X。
String.prototype.isPID = function () { return this.regex(/(^\d{15}$)|(^\d{17}([0-9]|X|x)$)/); }
//是否为区号个格式
String.prototype.isCode = function () { return this.regex(/^0\d{2,3}$/); }
//是否为有效天数
String.prototype.isOverDay = function () { return this.regex(/^(3[0-5]\d|36[0-5]|[0-2]?\d\d?)$/); }
//是否有包含特殊字符
String.prototype.isSpChar = function () { return !this.regex(/(<|>)/); }
String.prototype.isScript = function () { return this.regex(/(<[\/]?script.*>)/i); }
//是否为邮政编码格式
String.prototype.isZip = function () { return this.regex(/^\d{6}$/); }
//是否为网址格式
String.prototype.isUrl = function () { return this.regex(/^([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$/); }
//QQ,MSN,ICQ选择验证
function QMIVaildata(str, id, strValue) {
    switch (ID$(str).value) {
        case "3": show(id, strValue.isNull() || strValue.isQQ()); break;
        case "2": show(id, strValue.isNull() || strValue.isMSN()); break;
        case "1": show(id, strValue.isNull() || strValue.isICQ()); break;
        default: show(id, false); break;
    }
}
//是否为QQ格式
String.prototype.isQQ = function () { return this.regex(/^[1-9]\d{4,11}$/); }
//是否为MSN格式
String.prototype.isMSN = function () { return this.isEmail(); }
//是否为ICQ格式
String.prototype.isICQ = function () { return this.isQQ(); }
//是否为日期格式
String.prototype.isDate = function () { return this.regex(/^\d{1,4}(-|\/|\.|年)(0?[1-9]|1[0-2])(-|\/|\.|月)(0?[1-9]|[12]\d|3[01])(日)?$/); }
//去空格  返回值:去空后的字符串
//删除左右两端的空格
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, ""); }
//大于０的整数
String.prototype.isPinFang = function () { return this.regex(/^[1-9]\d*$/); }
//是否是正确的图片格式
String.prototype.isImage = function () { return this.toLowerCase().regex(/\.(jpg|gif)$/); }
//必须输入数字字符并且遇到非数字即可自动清除非数字
function inputCheckNumber(id, value) { if (!value.regex(/^[1-9]\d*$/)) ID$(id).value = value.replace(/\D/, ""); }
//是否只由英文字母和数字和-组成
String.prototype.isNumberLetter = function () { return this.regex(/^[0-9a-z\-]+$/); }
//让可选项失去焦点时，如果内容为空失去焦点时就显示无色
function secondTxtShow(id, value, exPress) {
    if (exPress) {
        show(id, true);
    } else if (!exPress && !value.isNull()) {
        show(id, false);
    } else {
        setStyle(id, "white", "#D4C0D8");
    }
}
//=============================其他函数======================================
//根据ID获取对象
function ID$(al) {
    if (document.getElementById) {
        return eval('document.getElementById("' + al + '")');
    } else if (document.layers) {
        return eval("document.layers['" + al + "']");
    } else {
        return eval('document.all.' + al);
    }
}

//1.我们首先创建一个全局变量xmlHttp
//2.我们在创建一个XmlHttpRequest对象
//var msxmls = new Array('Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP');
//var xmlhttp = getRequest();

//限制文本输入个数 复制时也是调用此方法
function maxTxt(id, maxChars) {
    if (parseInt(ID$(id).value.length) > parseInt(maxChars))
        ID$(id).value = ID$(id).value.substring(0, parseInt(maxChars));
}
//显示层
function showDiv(al) { ID$(al).style.display = "block"; }
//隐藏层
function hideDiv(al) { ID$(al).style.display = "none"; }

/* 用途：字符1是否包含字符串2 
输入：str1：字符串；str2：被包含的字符串 
返回：如果通过验证返回true,否则返回false */
function isMatch(str1, str2) { return (str1.indexOf(str2) == -1) ? false : true; }
/* 用途：字符1是否包含字符串2 
输入：str1：字符串；str2：被包含的字符串 
返回：如果通过验证返回true,否则返回false */
function isEqual(str1, str2) { return (str1 == str2) ? true : false; }
//全选和反选
checkobj = function (fun) {
    var el = document.getElementsByTagName('input');
    var len = el.length;
    for (var i = 0; i < len; i++) {
        if (el[i].type == "checkbox") fun(el[i]);
    }
}
function checkAll() { checkobj(function (obj) { obj.checked = true; }); }
function clearAll() { checkobj(function (obj) { obj.checked = !obj.checked; }); }
/* 用途：判断是否是日期 返回：如果通过验证返回true,否则返回false */
String.prototype.isEffectiveDate = function () {
    return this.regex(/^((((19|20)\d{2})(0?[13-9]|1[0-2])(0?[1-9]|[12]\d|30))|(((19|20)\d{2})(0?[13578]|1[02])31)|(((19|20)\d{2})0?2(0?[1-9]|1\d|2[0-8]))|(((19|20)(0[48]|[2468][048]|[13579][26])|(2000))0?229))$/);
}

/* 用途：字符1是否以字符串2结束 
输入：str1：字符串；str2：被包含的字符串 
返回：如果通过验证返回true,否则返回false  */
function isLastMatch(str1, str2) { return (str1.length == str1.lastIndexOf(str2) + str2.length) ? true : false; }
/* 用途：字符1是否以字符串2开始 
输入：str1：字符串；str2：被包含的字符串 
返回：如果通过验证返回true,否则返回false */
function isFirstMatch(str1, str2) { return (str1.indexOf(str2) > -1) ? true : false; }
/* 用途：检查输入的起止日期是否正确，规则为两个日期的格式正确， 
且结束如期>=起始日期 
输入： startDate：起始日期，字符串 ,endDate：结束如期，字符串 
返回： 如果通过验证返回true,否则返回false */
function checkTwoDate(startDate, endDate) { return (!startDate.isEffectiveDate() || !endDate.isEffectiveDate() || (startDate > endDate)) ? false : true; }

//==============================文本框 验证提示==================================


