`
sungang_1120
  • 浏览: 310954 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
收藏列表
标题 标签 来源
JS验证工具 js
function winOpen (strURL,strName,width,height)
{
    theWindow = window.open (strURL,strName,"width="+width+" height="+height+" scrollbars=yes left="+(1024-width)/2+" top="+(768-height)/2);	
    if (theWindow.opener == null) theWindow.opener = window;
    if (window.focus) theWindow.focus();
}
//验证邮件
function verifyEmailAddress(strEmail){
  var myReg = /^[_a-zA-Z0-9_-_._-]+@([_a-zA-Z0-9_-]+\.)+[a-zA-Z]{2,3}$/;
  return myReg.test(strEmail);
}
/*****************************************************************
****                     判断是否为日期数据  (lhm)       例子:itIsDate("2009-10-7" , "-")    *****
*****************************************************************/
function itIsDate(DateString , Dilimeter) 
{ 
  if (DateString==null) return false; 
  if (Dilimeter=='' || Dilimeter==null) 
   Dilimeter = '-'; 
  var tempy=''; 
  var tempm=''; 
  var tempd=''; 
  var tempArray; 
  if (DateString.length<8 && DateString.length>10) 
    return false;    
  tempArray = DateString.split(Dilimeter); 
  if (tempArray.length!=3) 
   return false; 
  if (tempArray[0].length==4) 
  { 
   tempy = tempArray[0]; 
   tempd = tempArray[2]; 
  } 
  else 
  { 
   tempy = tempArray[2]; 
   tempd = tempArray[1]; 
  } 
  tempm = tempArray[1]; 
  var tDateString = tempy + '/'+tempm + '/'+tempd+' 8:0:0';//加八小时是因为我们处于东八区 
  var tempDate = new Date(tDateString); 
  if (isNaN(tempDate)) 
   return false; 
 if (((tempDate.getUTCFullYear()).toString()==tempy) && (tempDate.getMonth()==parseInt(tempm)-1) && (tempDate.getDate()==parseInt(tempd))) 
  { 
   return true; 
  } 
  else 
  { 
   return false; 
  } 
} 

/*****************************************************************
****                   求字符串的字节长度     (lhm)          *****
*****************************************************************/
function byteLength(paraString) 
{
 var strValue =new String(paraString);
 var strLength = strValue.length;
 var numLength =0;
  for (globle_i =0 ; globle_i<strLength;globle_i++){
    var ASCIIValue =strValue.charCodeAt(globle_i);
    if ( ASCIIValue > 0 && ASCIIValue < 127 )  
      numLength = numLength + 1 
    else
     numLength = numLength + 2
  }
  return numLength;
}

/*****************************************************************
****                     去除空格     (lhm)                 *****
*****************************************************************/
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
	
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
		
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");}
String.prototype.ltrim = function() {return this.replace(/^\s+/,"");}
String.prototype.rtrim = function() {return this.replace(/\s+$/,"");}
/*****************************************************************
****               复选框的全选与取消     (LHM)              *****
*****************************************************************/
function CheckAll(form){
	var length = form.itemId.length;
	var tocheck = form.chkall.checked;
	if (length)
		for (var i=0; i<length; i++){ 
			if (form.itemId[i].disabled != true){
				form.itemId[i].checked = tocheck;
			} 			
		}
	else {
		if (form.itemId.disabled !=true){
			form.itemId.checked = tocheck;
		}
	}
}

/*****************************************************************
****                     删除处理     (LHM)                  *****
*****************************************************************/
function del_btn (form,strMsg,actionurl){
  	var result = false;
  	var length = form.itemId.length;	
	if (form.itemId.checked) { //只有一条记录时执行此语句
		result = true;	
	}  		
	for (var i=0; i<length; i++){ 
		if (form.itemId[i].checked){
		  result = true;
		  break;
		}		 		
	}
    if (!result){
		alert ("没有选择任何项目!");
		return false;
    }else{
		if (confirm('\n'+strMsg)){
			form.action = actionurl;
			return true;
		}
	    return false;
	} 	
}

/*****************************************************************
****                    转化字符串     (LHM)                 *****
*****************************************************************/
function conversion_code(paraString)
{
	strResult = "";
	j=0;
	for (i=0;i<paraString.length;i++){ 
		Char = String1.charAt(i);
		if (Char=="'"){
		    strResult = strResult + paraString.substring(j,i)+"\\"+"\'";
		    j=i+1;
		 } 
	return strResult;
	}
}

/*****************************************************************
****                 数字输入控制处理     (LHM)              *****
*****************************************************************/
function InputIntNumberCheck(){
	//为支持IE 或 Netscape
	var theEvent=window.event || arguments.callee.caller.arguments[0]; 
	var elm ;
	var ver = navigator.appVersion;
	if (ver.indexOf("MSIE") != -1){  // IE
		if ( !((theEvent.keyCode >=48)&&(theEvent.keyCode<=57))){
			theEvent.keyCode=0;
		}
	}else{ // Netscape
		if ( !((theEvent.which >=48)&&(theEvent.which<=57))){
			theEvent.stopPropagation();
			theEvent.preventDefault();
		}
	}
	//
}

/*****************************************************************
****          有小数点数字输入控制处理     (LHM)             *****
*****************************************************************/
function InputLongNumberCheck(){
	if ( !((window.event.keyCode >=48)&&(window.event.keyCode<=57) || window.event.keyCode ==46)){
		window.event.keyCode=0;
	}

	var theEvent=window.event || arguments.callee.caller.arguments[0]; 
	var elm ;
	var ver = navigator.appVersion;
	if (ver.indexOf("MSIE") != -1){  // IE
		if (!((theEvent.keyCode>=48)&&(theEvent.keyCode<=57) || theEvent.keyCode ==46)){
			theEvent.keyCode=0;
		}
	}else{ // Netscape
		if ( !((theEvent.which >=48)&&(theEvent.which<=57) || theEvent.which ==46)){
			theEvent.stopPropagation();
			theEvent.preventDefault();
		}
	}
}

/*****************************************************************
****                        换页处理                         *****
*****************************************************************/
function toWhichPage(objform, whichPage){
    objform.whichPage.value = whichPage;
    objform.submit();
}

/*************************liuxch   *******************************
****                        获取cookie内容                   *****
*****************************************************************/
function getCookie( name ){
	var nameOfCookie = name + "=";
	var x = 0;
	while ( x <= document.cookie.length ){
		var y = (x+nameOfCookie.length);
		if ( document.cookie.substring( x, y ) == nameOfCookie ) {
			if ( (endOfCookie=document.cookie.indexOf( ";", y )) == -1 )
			endOfCookie = document.cookie.length;
			return unescape( document.cookie.substring( y, endOfCookie ) );
		}
		x = document.cookie.indexOf( " ", x ) + 1;	
		if ( x == 0 ) break;
	}
	return "";
}

/*****************************************************************
****                    设置cookie内容、过期时间              *****
*****************************************************************/
function setCookie( name, value, expiredays ) { 
	var todayDate = new Date(); 
	todayDate.setDate( todayDate.getDate() + expiredays ); 
	document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";" 
} 
/*****************************************************************
****                      检查输入字符    (lhm)              *****
'//		 islegality:输入的字符是否为给定的字符
'//返回值:bool
*****************************************************************/
function islegality(checkstrpass){
var checkokpass="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
for (i=0; i<checkstrpass.length; i++) {
       ch=checkstrpass.charAt(i);
       for (j=0;j<checkokpass.length; j++){
        if (ch==checkokpass.charAt(j))
        break;
        }
      if (j==checkokpass.length){
	  return false; //函有特别字符时返回false
      break;
        }
  }
   return true;
}
/**
* 检查输入是否中文
*/
function ck_chinese(value_) {
  return escape(value_).indexOf("%u")!=-1 
}
纯JS面向对象表单验证 js
function Formfield(name, label){
	this.name = name;
	this.label = label;
}
function verifyForm(objForm){
	
	tinyMCE.triggerSave();//手动把iframe的值赋给textarea表单元素( 这个是根据自己使用的编辑器来定)
	
	var list  = new Array(
			new Formfield("name", "产品名称"),
			new Formfield("productTypeId", "产品类型"),
			new Formfield("baseprice", "产品底价"),
			new Formfield("marketprice", "产品市场价"),
			new Formfield("sellprice", "产品销售价"),
			new Formfield("description", "产品描述"),
			new Formfield("stylename", "产品图片的样式"),
			new Formfield("imagefile", "产品图片")
	);
	
	for(var i = 0; i < list.length; i++){
		
		var objfield = eval("objForm."+ list[i].name);
		
		if(trim(objfield.value)==""){
			
			alert(list[i].label+ "不能为空");
			
			if(objfield.type!="hidden" && objfield.focus()) objfield.focus();
			return false;
		}
	}
	
	
	var imagefile = objForm.imagefile.value;
	var ext = imagefile.substring(imagefile.length-3).toLowerCase();
	
	if (ext != "jpg" && ext != "gif" && ext != "bmp" && ext != "png"){
		
		alert("只允许上传gif、jpg、bmp、png!");
		
		return false; 
	}
    return true;
}
function SureSubmit(objForm){
	if (verifyForm(objForm)) 
		objForm.submit();
}


 <input type="button" name="Add" value=" 确 认 " class="frm_btn" onClick="javascript:SureSubmit(this.form)">
纯JS Ajax提交 js
function send_request(callback, urladdress, isReturnData){      
        var xmlhttp = getXMLHttpRequest();
        xmlhttp.onreadystatechange = function(){
            	if (xmlhttp.readyState == 4) {//readystate 为4即数据传输结束
 				    try{
				    	if(xmlhttp.status == 200){
							if(isReturnData && isReturnData==true){
								callback(xmlhttp.responseText);
							}
						}else{
							callback("抱歉,没找到此页面:"+ urladdress +"");
						}
			        } catch(e){
			        	callback("抱歉,发送请求失败,请重试 " + e);
			        }
			   }
        };
        //get代表提交方式
        //true代表是异步请求
        xmlhttp.open("GET", urladdress, true);
        xmlhttp.send(null);
}




function getXMLHttpRequest() {
        var xmlhttp = null;
		if (window.XMLHttpRequest) {
			try {
				xmlhttp = new XMLHttpRequest();
				xmlhttp.overrideMimeType("text/html;charset=UTF-8");//设定以UTF-8编码识别数据
			} catch (e) {}
		} else if (window.ActiveXObject) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				try {
					xmlhttp = new ActiveXObject("Msxml2.XMLHttp");
				} catch (e) {
					try {
						xmlhttp = new ActiveXObject("Msxml3.XMLHttp");
					} catch (e) {}
				}
			}
		}
        return xmlhttp;
}



实例:

function checkUsername(){
		var form = document.forms[0];
		var username = form.username.value;
		var viewobj = document.getElementById("checkResult");
		viewobj.innerHTML = "正在检测中...";
                //true是否接收返回数据
		send_request(function(value){viewobj.innerHTML=value},
		'<html:rewrite action="/user/reg"/>?method=isUserExsit&username='+ username, true);
}
Global site tag (gtag.js) - Google Analytics