日期:2014-05-16 浏览次数:20518 次
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 = nu