//全角转半角
//obj为要转换
function ToCDB(obj) {
//37 //39 //46 //8
if(event.keyCode ==37 || event.keyCode ==39 || event.keyCode ==46 ||event.keyCode ==8 ){
return;
}
var str = obj.value;
var tmp = "";
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) > 65248 && str.charCodeAt(i) < 65375) {
tmp += String.fromCharCode(str.charCodeAt(i) - 65248);
} else {
tmp += String.fromCharCode(str.charCodeAt(i));
}
}
obj.value=tmp;
}
//是否符合邮政编码格式:6位数字
function isZipcode(value){
var pattern = /^\d{6}$/;
return pattern.test(value);
}
//是否是固定电话号码
//1.单位电话区号首位必须以“0” 开头。 2.单位电话分机号为3-8位数字或为空。
function isPhone(value) {
var shortPattern = /^0\d{2,3}[-]{1}\d{7,8}$/;
var longPattern = /^0\d{2,3}[-]{1}\d{7,8}[-]{1}\d{3,8}$/;
return (shortPattern.test(value) || longPattern.test(value)) && !isSameNumbers(value);
}
//是否是移动电话
function isMobile(value) {
var mobilePattern = /^[1]{1}\d{10}$/;
return mobilePattern.test(value) && !isSameNumbers(value);
}
//判断电话号码中是否数字全重复
function isSameNumbers(value) {
var numbers = value.replace(/\-/g, "");
var index = 0;
var count = 0;
while ((index+1) < numbers.length) {
count = count + Math.abs(numbers.charAt(index) - numbers.charAt(index + 1));
if (count > 0) {
break;
}
index = index + 1;
}
return count == 0 ? true : false;
}
/**
* 校验客户姓名:不含*、数字、字母、空格、产品名称
* @param name
* @return true:格式正确;false:格式不正确
* @author YAOFAN972
*/
function checkCustName(name){
//客户姓名生僻字用拼音录入,允许输入大写字母A~Z
// var regx = /[A-Za-z0-9\*\s\uFF00-\uFFFF\u3000\uE000-\uF8FF×]+|畅行天下+|倍保安行+|金灿人生+|倍保如意+|倍保无忧+|得益人生+|乐逸人生+|乐翔人生+|安行无忧+|中国平安+|中国移动+|倍保如意\2010\+|先生+|小姐+|女士+/;
var regx = /[a-z0-9\*\s\uFF00-\uFFFF\u3000\uE000-\uF8FF×]+|畅行天下+|倍保安行+|金灿人生+|倍保如意+|倍保无忧+|得益人生+|乐逸人生+|乐翔人生+|安行无忧+|鸿运如意+|鸿运安行+|馨逸人生+|康逸人生+|平安出行+|安益人生+|中国平安+|中国移动+|鸿运英才+|倍保如意\2010\+|先生+|小姐+|女士+/;
return !regx.test(name);
}
/**
* 统一规则:省+市+区+细址 总长度 不能超过100个字符。
* @param provinceID,cityID,countryID,addressID
* 省ID,市ID,区ID,细址ID,提示信息在细址后。
* @return 校验成功返回:true;校验失败返回:false
* @author YAOFAN972
* 为了四项和五项能同时校验,ex-zhuqimeng001于2011-10-31 对此修改。
**/
function checkAddress(){
var args_len = arguments.length;
var args = arguments;
if(args_len == 5){
return checkAddress5items(args[0],args[1],args[2],args[3],args[4]);
}else if( args_len == 4){
return checkAddress4items(args[0],args[1],args[2],args[3]);
}else {
return false;
}
}
function checkAddress5items(provinceID,cityID,countryID,streetId,addressID){
var provinceLength=0;
var cityLength=0;
var countyLength=0;
var streetLength = 0;
var provinceCombo = document.getElementById(provinceID);
var province = provinceCombo.options[provinceCombo.selectedIndex].text;
if(provinceCombo.options[provinceCombo.selectedIndex].value!=""){
provinceLength=paGetLengthWithoutTrim(province);
}
var cityCombo = document.getElementById(cityID);
var city = cityCombo.options[cityCombo.selectedIndex].text;
if(cityCombo.options[cityCombo.selectedIndex].value!=""){
cityLength=paGetLengthWithoutTrim(city);
}
var countryCombo = document.getElementById(countryID);
var county = countryCombo.options[countryCombo.selectedIndex].text;
if(countryCombo.options[countryCombo.selectedIndex].value!=""){
countyLength=paGetLengthWithoutTrim(county);
}
var streetCombo = document.getElementById(streetId);
var street = streetCombo.options[streetCombo.selectedIndex].text;
if(streetCombo.options[streetCombo.selectedIndex].value!=""){
streetLength=paGetLengthWithoutTrim(street);
}
//alert (provinceLength +"+"+ cityLength +"+"+ countyLength );
var length = provinceLength + cityLength + countyLength + streetLength + paGetLengthWithoutTrim($("#"+addressID).val());
//alert(length);
if(length > 100){
showMsg(addressID,"联系地址省市区乡镇街道加细址长度不能超过100个字符。");
return false;
}else{
hintMsg(addressID);
return true;
}
}
function checkAddress4items(provinceID,cityID,countryID,addressID){
var provinceLength=0;
var cityLength=0;
var countyLength=0;
var provinceCombo = document.getElementById(provinceID);
var province = provinceCombo.options[provinceCombo.selectedIndex].text;
if(provinceCombo.options[provinceCombo.selectedIndex].value!=""){
provinceLength=paGetLengthWithoutTrim(province);
}
var cityCombo = document.getElementById(cityID);
var city = cityCombo.options[cityCombo.selectedIndex].text;
if(cityCombo.options[cityCombo.selectedIndex].value!=""){
cityLength=paGetLengthWithoutTrim(city);
}
var countryCombo = document.getElementById(countryID);
var county = countryCombo.options[countryCombo.selectedIndex].text;
if(countryCombo.options[countryCombo.selectedIndex].value!=""){
countyLength=paGetLengthWithoutTrim(county);
}
//alert (provinceLength +"+"+ cityLength +"+"+ countyLength );
var length = provinceLength + cityLength + countyLength + paGetLengthWithoutTrim($("#"+addressID).val());
//alert(length);
if(length > 100){
showMsg(addressID,"联系地址省市区加细址长度不能超过100个字符。");
return false;
}else{
hintMsg(addressID);
return true;
}
}
/**
* 执行test正则
* @param val 值
* @param reg 正则表达式
* @return
*/
function paTestReg(val, reg) {
return reg.test(val);
}
/**
* 得到长度,清空2端空格等(汉字为2个字节长度)
* @param val 值
* @return 返回长度
*/
function paGetLength(val) {
if (!val) {
return 0;
}
val = paTrim(val);
if (val.length <= 0) {
return 0;
}
return val.replace(/[^\x00-\xff]/g, "**").length;
}
/**
* 得到长度(汉字为2个字节长度)
* @param val 值
* @return 返回长度
*/
function paGetLengthWithoutTrim(val) {
if (!val) {
return 0;
}
if (val.length <= 0) {
return 0;
}
return val.replace(/[^\x00-\xff]/g, "**").length;
}
/**
* 清空2端空格等
* @param val 值
* @return
*/
function paTrim(val) {
if (!val) {
return val;
}
return val.replace(/(^\s*)|(\s*$)/g, "");
}
/**
* 判断是否为空,清空2端空格等
* @param val 值
* @return
*/
function paEmpty(val) {
if (!val) {
return true;
}
return paTrim(val).length > 0 ? false : true;
}
/**
* 判断是否闰年
* @param year
* @return true:闰年;false:非闰年
*/
function paLeapYear(year) {
return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? true : false;
}
/**
* 得到XX年XX月的最大天数
* @param year 年
* @param month 月
* @return
*/
function paGetMaxDay(year, month) {
var daysArray = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if (month != 2) {
return daysArray[month - 1];
}
return paLeapYear(year) ? 29 : 28;
}
/**
* 左补零
* @param val 值
* @param len 希望补零后的长度
* @return
*/
function paLeftFillZero(val, len) {
var ll = val.length;
if (ll >= len) {
return val;
}
var num = len - ll;
for (var i = 0; i < num; i++) {
val = "0" + val;
}
return val;
}
/**
* 判断是否数字
* @param val 值
* @return
*/
function paNumeric(val) {
if (val != "" && val !="0") {
var reg = /^[1-9]{1}[\d]*$/;
return paTestReg(val, reg);
}
return true;
}
/**
* 检查时间大小
* @param starttime
* @param endtime
* @returns {Boolean}
*/
function checkTimeBefore(starttime,endtime) {
//var starttime = $("#starttime").val();
//var endtime = $("#endtime").val();
var istart = parseInt(starttime.split(":")[0]) * 100
+ parseInt(starttime.split(":")[1]);
var iend = parseInt(endtime.split(":")[0]) * 100
+ parseInt(endtime.split(":")[1]);
if (iend < istart) {
return false;
} else {
return true;
}
}
/**
* 判断是时间格式 24小时制时间
*/
function pa24Time(val) {
var reg = /^([01][0-9]|2[0-3])\:[0-5][0-9]$/
return paTestReg(val, reg);
}
/**
* 判断是否汉字
* @param val 值
* @return
*/
function paChinese(val) {
var re = new RegExp("^[\\u4e00-\\u9fa5]+$", "");
return re.test(val);
}
/**
* 判断是否半角字符
* @param val 值
* @return
*/
function paHalfCharacter(val) {
var re = new RegExp("^[\x00-\xff]+$", "");
return re.test(val);
}
/**
* 判断是否字母
* @param val
* @return
*/
function paEnglish(val) {
var re = new RegExp("^[a-zA-Z]+$", "");
return re.test(val);
}
/**
* 日期校验
* @param obj
* @return
*/
function f_check_date(obj) {
var date = Trim(obj.value);
if (date.length == 0) {
return true;
}
var dtype = obj.dataType;
if (dtype.indexOf("(") == -1) {
f_alert(obj, "\u8bf7\u6307\u5b9a\u65e5\u671f\u683c\u5f0f");
return false;
}
var format = dtype.substring(dtype.indexOf("(") + 1, dtype.indexOf(")")); //日期格式
var year, month, day, datePat, matchArray;
if (/^(y{4})(-|\/)(M{1,2})\2(d{1,2})$/.test(format)) {
datePat = /^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})(\s((0[0-9])|([2][0-3])|[1][0-9])\:([0-5][0-9]))?$/;
} else {
if (/^(y{4})(年)(M{1,2})(月)(d{1,2})(日)$/.test(format)) {
datePat = /^(\d{4})年(\d{1,2})月(\d{1,2})日(\s((0[0-9])|([2][0-3])|[1][0-9])\:([0-5][0-9]))?$/;
} else {
if (format == "yyyyMMdd") {
datePat = /^(\d{4})(\d{2})(\d{2})(\s((0[0-9])|([2][0-3])|[1][0-9])\:([0-5][0-9]))?$/;
} else {
//f_alert(obj, "\u65e5\u671f\u683c\u5f0f\u4e0d\u5bf9");
//return false;
}
}
}
matchArray = date.match(datePat);
if (matchArray == null) {
f_alert(obj, "\u975E\u6709\u6548\u65E5\u671F\u683C\u5F0F");
return false;
}
if (/^(y{4})(-|\/)(M{1,2})\2(d{1,2})$/.test(format)) {
year = matchArray[1];
month = matchArray[3];
day = matchArray[4];
} else {
year = matchArray[1];
month = matchArray[2];
day = matchArray[3];
}
if (month < 1 || month > 12) {
f_alert(obj, "\u6708\u4efd\u5e94\u8be5\u4e3a1\u523012\u7684\u6574\u6570");
return false;
}
if (day < 1 || day > 31) {
f_alert(obj, "\u6bcf\u4e2a\u6708\u7684\u5929\u6570\u5e94\u8be5\u4e3a1\u523031\u7684\u6574\u6570");
return false;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
f_alert(obj, "\u8be5\u6708\u4e0d\u5b58\u572831\u53f7");
return false;
}
if (month == 2) {
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29) {
f_alert(obj, "2\u6708\u6700\u591a\u670929\u5929");
return false;
}
if ((day == 29) && (!isleap)) {
f_alert(obj, "\u95f0\u5e742\u6708\u624d\u670929\u5929");
return false;
}
}
return true;
}
/**
* 日期校验
* @param obj
* @return
*/
function isValidDate(obj) {
var date = Trim(obj.value);
if (date.length == 0) {
return true;
}
var dtype = obj.dataType;
if (dtype.indexOf("(") == -1) {
return false;
}
var format = dtype.substring(dtype.indexOf("(") + 1, dtype.indexOf(")")); //日期格式
var year, month, day, datePat, matchArray;
if (/^(y{4})(-|\/)(M{1,2})\2(d{1,2})$/.test(format)) {
datePat = /^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})(\s((0[0-9])|([2][0-3])|[1][0-9])\:([0-5][0-9]))?$/;
} else {
if (/^(y{4})(年)(M{1,2})(月)(d{1,2})(日)$/.test(format)) {
datePat = /^(\d{4})年(\d{1,2})月(\d{1,2})日(\s((0[0-9])|([2][0-3])|[1][0-9])\:([0-5][0-9]))?$/;
} else {
if (format == "yyyyMMdd") {
datePat = /^(\d{4})(\d{2})(\d{2})(\s((0[0-9])|([2][0-3])|[1][0-9])\:([0-5][0-9]))?$/;
} else {
return false;
}
}
}
matchArray = date.match(datePat);
if (matchArray == null) {
return false;
}
if (/^(y{4})(-|\/)(M{1,2})\2(d{1,2})$/.test(format)) {
year = matchArray[1];
month = matchArray[3];
day = matchArray[4];
} else {
year = matchArray[1];
month = matchArray[2];
day = matchArray[3];
}
if (month < 1 || month > 12) {
return false;
}
if (day < 1 || day > 31) {
return false;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
return false;
}
if (month == 2) {
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29) {
return false;
}
if ((day == 29) && (!isleap)) {
return false;
}
}
return true;
}
/*LTrim(string):去除左边的空格*/
function LTrim(str) {
var whitespace = new String(" \t\n\r");
var s = new String(str);
if (whitespace.indexOf(s.charAt(0)) != -1) {
var j = 0, i = s.length;
while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
j++;
}
s = s.substring(j, i);
}
return s;
}
/*RTrim(string):去除右边的空格*/
function RTrim(str) {
var whitespace = new String(" \t\n\r");
var s = new String(str);
if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
var i = s.length - 1;
while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
i--;
}
s = s.substring(0, i + 1);
}
return s;
}
/*Trim(string):去除字符串两边的空格*/
function Trim(str) {
return RTrim(LTrim(str));
}
/* 弹出警告对话框,用户点确定后将光标置于出错文本框上, 并且将原来输入内容选中。*/
function f_alert(obj, alertInfo) {
var caption = obj.getAttribute("msg");
var type = obj.getAttribute("type");
if (caption == null) {
caption = "";
}
alert(caption + "\uff1a" + alertInfo + "\uff01");
if (type != null) {
if (type == "text" || type == "TEXT" || type == "textarea" || type == "TEXTAREA") {
obj.select();
}
}
if (isVisible(obj) && checkPrVis(obj)) {
obj.focus();
}
}
/*判断当前对象是否可见*/
function isVisible(obj) {
var visAtt, disAtt;
try {
disAtt = obj.style.display;
visAtt = obj.style.visibility;
}
catch (e) {
}
if (disAtt == "none" || visAtt == "hidden") {
return false;
}
return true;
}
/*判断当前对象及其父对象是否可见*/
function checkPrVis(obj) {
var pr = obj.parentNode;
do {
if (pr == undefined || pr == "undefined") {
return true;
} else {
if (!isVisible(pr)) {
return false;
}
}
} while (pr = pr.parentNode);
return true;
}
/**
*使用jquery trim 函数
*@param id 用$("#id").val()获得值
*@return 返回去掉字符串起始和结尾的空格后的值
*/
function jqueryTrim(id){
return $.trim($("#"+id).val())
}
/**
*使用jquery trim 函数去掉字符串起始和结尾的空格
*然后将去空格后的值设置回去
*@param id 用$("#id").val()获得值
*
*/
function jqueryTrimReset(id){
$("#"+id).val($.trim($("#"+id).val()))
}
}