- js常用方法将会不断更新
- /*
- 说明:去除字符串两边空格函数
- 参数obj:要去除空格的文本框
- 返回值:去除空格之后的字符串
- */
- function trim(obj) {
- return String(obj.value).replace(/(^\s*)|(\s*$)/g, "");
- }
- /*
- 说明:显示错误信息函数
- 参数obj:出现错误信息的文本框
- 参数errmsg:错误信息
- */
- function showError(obj, errmsg) {
- alert(errmsg);
- try{
- obj.focus();
- } catch(e) {
- }
- }
- /*
- 说明:检查是否为空函数
- 参数obj:要检查的文本框
- 返回值:判断结果 true不为空 false为空
- */
- function checkEmpty(obj) {
- if(obj == "") {
- return false;
- } else {
- return true;
- }
- }
- /*
- 说明:检查长度函数
- 参数obj:要检查长度的文本框
- 参数min:最小长度
- 参数max:最大长度
- 返回值:判断结果 true在要求长度中 false超出要求长度
- */
- function checkLength(obj, min, max) {
- if(obj.length < min || obj.length > max) {
- return false;
- } else {
- return true;
- }
- }
- /*
- 说明:下拉列表选中函数
- 参数obj:要选中的下拉列表
- 参数selectvalue:标识选中的参数
- */
- function selectitem(obj , selectvalue){
- var options = obj.options;
- for(var i = 0; i < options.length; i++) {
- if(selectvalue == options[i].value) {
- options[i].selected = true;
- }
- }
- }
- /*
- 说明:判断value变量值是否是数字
- 参数value:输入值
- 返回值:是数字返回true,否则false
- */
- function isNumeric(value){
- if( value != null && value.length>0 && isNaN(value) == false){
- return true;
- }
- else{
- return false;
- }
- }
- /*
- 说明:判断value变量值是否是中文
- 参数value:输入值
- 返回值:是中文返回false,否则true
- */
- function isChn(str){
- var reg = /^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$/;
- if(reg.test(str)){
- return false;
- }
- return true;
- }
- /*
- 说明:对复选框的全选或不选
- 参数state:输入值 1 全选 2 全部选
- 返回值:是中文返回false,否则true
- */
- function change(state){
- try{
- var checks=document.getElementsByTagName("input");
- var i=0;
- var length=checks.length;
- var flag=true;
- if(state==1){
- flag=true;
- }
- if(state==0){
- flag=false;
- }
- for(i;i<length;i++){
- if(checks[i].type=="checkbox"){
- checks[i].checked=flag;
- }
- }
- }catch(e){
- window.alert(e.message);
- }
- }
- /**
- * <code>DateUtil</code>类用于封装常用的日期处理操作
- */
- var DateUtil = function(year,month,day,hour,minute,second){
- /** curDateTime 当前客户端日期时间*/
- this.curDateTime = new Date();
- /**
- * <code>getDateTime</code>方法返回Date类型对象
- *
- */
- this.getDateTime = function(){
- var date = null;
- if((year==null && month==null && day==null
- && hour == null && minute == null && second == null)){
- date = this.curDateTime;
- }else if(year != null && month != null && day != null
- && hour == null && minute == null && second == null){
- date = new Date(year,month-1,day);
- }else if(year != null && month != null && day != null
- && hour != null && minute != null && second != null){
- date = new Date(year,month-1,day,hour,minute,second);
- }
- return date;
- };
- /**
- * <code>getYear</code>方法取得年值
- *
- */
- this.getYear = function(){
- var year = null;
- var dateTime = this.getDateTime();
- if(dateTime != null){
- year = dateTime.getFullYear();
- }else{
- year = this.curDateTime.getFullYear();
- }
- return year;
- };
- /**
- * <code>getMonth</code>方法取得月值
- *
- */
- this.getMonth = function(){
- var month = null;
- var dateTime = this.getDateTime();
- if(dateTime != null){
- month = dateTime.getMonth() + 1;
- }else{
- month = this.curDateTime.getMonth() + 1;
- }
- return month;
- };
- /**
- * <code>getDay</code>方法取得日值
- *
- */
- this.getDay = function(){
- var day = null;
- var dateTime = this.getDateTime();
- if(dateTime != null){
- day = dateTime.getDate();
- }else{
- day = this.curDateTime.getDate();
- }
- return day;
- };
- /**
- * <code>getHour</code>方法取得24进制小时
- *
- */
- this.getHour = function(){
- var hour = null;
- var dateTime = this.getDateTime();
- if(dateTime != null){
- hour = dateTime.getHours();
- }else{
- hour = this.curDateTime.getHours();
- }
- return hour;
- };
- /**
- * <code>getMinute</code>方法取得分值
- *
- */
- this.getMinute = function(){
- var minute = null;
- var dateTime = this.getDateTime();
- if(dateTime != null){
- minute = dateTime.getMinutes();
- }else{
- minute = this.curDateTime.getMinutes();
- }
- return minute;
- };
- /**
- * <code>getSecond</code>方法取得秒值
- *
- */
- this.getSecond = function(){
- var second = null;
- var dateTime = this.getDateTime();
- if(dateTime != null){
- second = dateTime.getSeconds();
- }else{
- second = this.curDateTime.getSeconds();
- }
- return second;
- };
- /**
- * <code>getDateRange</code>方法用于得到一天之内的时刻范围
- *
- * @return range ["凌晨"|"上午"|"中午"|"下午"|"晚上"]
- */
- this.getDateRange = function(){
- var hour = window.parseInt(this.getHour());
- var range = "凌晨"
- if(hour >= 6 && hour < 11){
- range = "早晨";
- }else if(hour >=11 && hour < 14){
- range = "中午";
- }else if(hour >=14 && hour <= 18){
- range = "下午";
- }else if(hour >18 && hour < 24){
- range = "晚上";
- }
- return range;
- };
- /**
- * <code>get12PatternHour</code>方法用于得到12进制小时值
- *
- */
- this.get12PatternHour = function(){
- return hour>12?(hour+12-24):hour;
- };
- /**
- * <code>isLeapYear</code>方法用于判断是否为闰年
- * <p>
- * 闰年算法说明:
- * 能被4整除并且不能被100整除或者能被400整除的年份是闰年
- */
- this.isLeapYear = function(){
- var flag = false;
- if((this.getYear() % 4 == 0 && this.getYear() % 100 !=0)
- || (this.getYear() % 400 == 0)){
- flag = true;
- }
- return flag;
- };
- /**
- * <code>getMaxDaysByMonth</code>方法根据月份获取该月的最大天数
- *
- */
- this.getMaxDaysByMonth = function(){
- var days = 31;
- var month = this.getMonth();
- switch(month){
- case 2:
- if(this.isLeapYear()){
- days = 29;
- }else{
- days = 28;
- }
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- days = 30;
- break;
- default:
- break;
- }
- return days;
- }
- }
- /**
- * <code>isEmptyString</code>方法用于检查字符串是否为空字符串
- * <p>
- * @return boolean false → 不是空串 true → 是空串
- */
- function isEmptyStr(str){
- if(trim(str).length == 0 || str==null){
- return true;
- }else{
- return false;
- }
- }
- /**
- * <code>isEqualString</code>方法用于检查两个字符串是否相等
- * <p>
- * @return boolean false → 不相等 true → 相等
- */
- function isEqualStr(str1,str2){
- if(str1 == str2){
- return true;
- }else{
- return false;
- }
- }
- /**
- * <code>isValidateCols</code>方法用于检查字符串是否是有效位数
- * <p>
- * @return boolean false → 不是制定位数 true → 是指定位数
- */
- function isValidateMinCols(str,cols){
- if(str.length >= cols){
- return true;
- }else{
- return false;
- }
- }
- function isValidateMaxCols(str,cols){
- if(str.length <= cols){
- return true;
- }else{
- return false;
- }
- }
- function isValidateRangeCols(str,min,max){
- if(str.length >= min
- && str.length <=max){
- return true;
- }else{
- return false;
- }
- }
- /**
- * <code>isValidateEmail<code>方法用于检查email格式是否正确
- * <p>
- * @return boolean false → 无效Email true → 有效Email
- */
- function isValidateEmail(email){
- var emailPattern = "^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))" +
- //"@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|NET|com|COM|gov|GOV|mil" +
- "@([a-zA-Z0-9-]+[.])+(cn|net|NET|com|COM|gov|GOV|mil" +
- "|MIL|org|ORG|edu|EDU|int|INT)$"
- var re = new RegExp(emailPattern);
- if(re.test(email)){
- return true;
- }else{
- return false;
- }
- }
- /**
- * <code>trim</code>方法用于去掉字符串两边的空格
- * <p>
- */
- function trim(str){
- str = trimLeft(trimRight(str));
- return str;
- }
- /**
- * <code>trimLeft</code>方法用于去除字符串左侧的空格
- * <p>
- * @param str 预处理的字符串
- * @return 去掉左侧空格的字符串
- */
- function trimLeft(str){
- var pattern = /^\s/;
- while(pattern.test(str)){
- str = str.substring(1);
- }
- return str;
- }
- /**
- * <code>trimRight</code>方法用于去除字符串右侧的空格
- * <p>
- * @param str 预处理字符串
- * @return 去掉右侧空格的字符串
- */
- function trimRight(str){
- var pattern = /\s$/;
- while(pattern.test(str)){
- str = str.substring(0,str.length-1);
- }
- return str;
- }