javascript 页面输入内容的检查与判断方法

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}

function lockNumber() {
	var str="0123456789";
	for (i=0;i<10;i++) {
		if(window.event.keyCode ==str.charCodeAt(i))
		return true;
	}
	window.event.returnValue =false;
}

function csNumberChk(slInputCode)
{
	slInputCode += "";

	if(slInputCode == ""){
		return true;
	}
	if(slInputCode.match(/^[1-9][0-9]*$|^0$/)){
		return true;
	} else {
		return false;
	}
}

function chkChar(slStr){


	slStr = slStr.trim();
	var ngChars = importChars();
	for(i = 0;i < slStr.length;i++) {
		for(j=0; j<ngChars.length; j++) {
			if(slStr.charCodeAt(i) == ngChars[j]){
				return slStr.charAt(i);
			}
		}
	}
	return "";
}


function importChars(){

/*	var sKinsoku = "8722;8721;8216;44;37;92;39;38;215;8741;9312;9313;9314;"
				 + "9315;9316;9317;9318;9319;9320;9321;9322;9323;9324;9325;9326;"
				 + "9327;9328;9329;9330;9331;8544;8545;8546;8547;8548;8549;8550;"
				 + "8551;8552;8553;13129;13076;13090;13133;13080;13095;13059;13110;"
				 + "13137;13143;13069;13094;13091;13099;13130;13115;13212;13213;13214;"
				 + "13198;13199;13252;13217;12317;12319;8470;13261;8481;12964;12965;"
				 + "12966;12967;12968;12849;12850;12857;13182;13181;13180;13179;8750;"
				 + "931;8735;8895";
*/
	var sKinsoku = "44;37;39";			
	var ngArray = new Array();
	ngArray = sKinsoku.split(';');

	return ngArray;
}

function getMessage(slCode,slMsgString,olParam) {
	if(typeof(olParam)=="string") {
		slMsgString=slMsgString.replace(/\{0\}/g,olParam);
	}
	else if(typeof(olParam) == "object" && olParam.constructor == Array) {
		slMsgString = slMsgString.replace(/\{/g,"\"+olParam\[");
		slMsgString = slMsgString.replace(/\}/g,"\]+\"");
		slMsgString = eval("\"" + slMsgString + "\"");

	}
	if(slMsgString != null && slMsgString != ""){
		slMsgString = "111111" + slCode
					+ "\r\n222222\r\n"
					+ slMsgString;
	}
	return slMsgString;
}
function csStringSize(slInput) {
    var nlCnt = 0;
    for (i=0; i<slInput.length; i++){
        clDore = escape(slInput.charAt(i));
        if (clDore.length < 4) {
            nlCnt++; 
        } else {
            if (clDore.indexOf("%uFF")!=-1 &&
            '0x'+clDore.substring(2,clDore.length) > 0xFF66 &&
            '0x'+clDore.substring(2,clDore.length) < 0xFFA0){
                nlCnt++;
            } else {
                nlCnt+=2;   
            }
        }
    }
    return nlCnt;
}

function csDateFmt(slDate) {
	var nlYear = 0;							
	var nlMonth = 0;				
	var nlDay = 0;						

	if(slDate.match(/^\d{1,4}\/\d{1,2}\/\d{1,2}$/)){
		var alDate = slDate.split("/");
		nlYear = alDate[0] - 0;
		nlYear = nlYear<100 ? (2000+nlYear) : nlYear;
		nlMonth = alDate[1] - 1;
		nlDay = alDate[2] - 0;
	}
	else if(slDate.match(/^\d{6}$/)){
		nlYear = "20" + slDate.substr(0,2) - 0;
		nlMonth = slDate.substr(2,2) - 1;
		nlDay = slDate.substr(4,2) - 0;
	}
	else if(slDate.match(/^\d{8}$/)){
		nlYear = slDate.substr(0,4) - 0;
		nlMonth = slDate.substr(4,2) - 1;
		nlDay = slDate.substr(6,2) - 0;
	}
	else{
		return "";
	}

	if(nlMonth < 0 || nlMonth > 11 || nlDay < 1 || nlDay > 31){
		return "";
	}

	var dlDate = new Date(nlYear, nlMonth, nlDay);
	if(dlDate.getFullYear() != nlYear || dlDate.getMonth() != nlMonth || dlDate.getDate() != nlDay){
		return "";
	}

	nlMonth++;
	return nlYear
			+ "/" + ( nlMonth < 10 ? ("0"+nlMonth) : nlMonth )
			+"/" + ( nlDay < 10 ? ("0"+nlDay) : nlDay );
}

function csDateFmtBlur(olItem) {
	var slDate = olItem.value.trim();

	if(slDate == "" || slDate == null || olItem.readOnly==true){
		return "0";
	}
	/*if(!chkChar(olItem)){
		return "3";
	}*/
	slDate = csDateFmt(slDate);
	if(slDate == ""){
		return "1";
	}

	/*if(slDate.substr(0,4) < 2000 || slDate.substr(0,4) > 2079){
		return "2";
	}*/

	olItem.value = slDate;
	return "0";
}

function csDateFmtFocus(olItem) {
	var slDate = olItem.value.trim();

	if(slDate != "" && slDate != null && olItem.readOnly == false) {
		olItem.value = slDate.replace(/\//g,"");
		olItem.select();
	}
}

function csNumFmt(slData){

	var nlValue = Number(String(slData).replace(/\,/g,"").trim());

	if(isNaN(nlValue)){
		return "";
	}

	if(nlValue < 0){
		nlValue = nlValue * -1;
		slData = "-";
	} else {
		slData = "";
	}

	var alTemArr = nlValue.toString().split(".");
	var alIntArr = new Array();

	for (var i = alTemArr[0].length ; i > 0 ; i -= 3){
		alIntArr.push(alTemArr[0].substring(i-3,i));
	}

	slData += alIntArr.reverse().join(",");

	if(alTemArr.length > 1){
		slData += "." + alTemArr[1];
	}

	return slData;
}

function csNumFmtBlur(olItem) {
	var slValue = olItem.value.trim();

	if(slValue == "" || slValue == null){
		return "0";
	}
	slValue = csNumFmt(olItem.value);


	if(slValue == ""){
		return "1";
	} else {
		olItem.value = slValue;
		return "0";
	}
}

function csNumFmtFocus(olItem) {
	var slValue = olItem.value.trim();

	if(slValue != "" && slValue != null && olItem.readOnly == false) {
		olItem.value = slValue.replace(/\,/g,"");
		olItem.select();
	}
}

function changeFrame(olItem){
	olItem = olItem.parentNode;

	var alNodes = olItem.parentNode.childNodes;
	var i = 0
	for(; i < alNodes.length;++i){
		//
		if(i%2){
			alNodes[i].className = "skin_tab_default_right2";
		} else {
			alNodes[i].className = "skin_tab_default_back";
		}
	}
	alNodes[i-1].className= "skin_tab_default_right";

	if(olItem.previousSibling != null){
		olItem.previousSibling.className = "skin_tab_default_right3";
	}

	olItem.className = "skin_tab_down_back";

	if(alNodes[i-2] == olItem){
		olItem.nextSibling.className = "skin_tab_down_right";
	} else {
		olItem.nextSibling.className = "skin_tab_down_right2";
	}
}


function chkRequired(slInput){
	return String(slInput).trim() != "";
}

function chkDate(slInput){
	return csDateFmt(slInput) != "";
}

function chkFromToDate(slDateFrom,slDateTo){

	slDateFrom = csDateFmt(slDateFrom).replace(/\//g,"");

	slDateTo = csDateFmt(slDateTo).replace(/\//g,"");

	return Number(slDateFrom) <= Number(slDateTo);
}

function chkStringMaxLength(slInput,nlMaxLen){
	//return csStringSize(slInput) <= nlMaxLen;
	if(typeof(slInput) == "string"){
		return slInput.length <= nlMaxLen;
	} else {
		return false;
	}
}

function chkNumeric(slInput){
	if(slInput != ""){
		return !isNaN(Number(slInput));
	} else {
		return false;
	}
}

function chkNumInter(slInput){
	if(slInput != ""){
		if(slInput.indexOf(".") != -1){
			return false;
		}

		if(!isNaN(Number(slInput))){
			return true;
		}
	} else {
		return false;
	}
}

function chkNumericRange(slNumer,slBegin,slEnd){

	if(typeof(slBegin) == "undefined" ||  slBegin == null){
		slBegin = 0;
	}

	if(typeof(slEnd) == "undefined" ||  slEnd == null){
		slEnd = 99999999999;
	}

	return Number(slNumer) >= Number(slBegin)
		&& Number(slNumer) <= Number(slEnd);
}

function chkAlphaNumeric(slInput){
	return slInput.match(/[^A-Za-z0-9]/g) == null;
}

function chkHankakuString(slInput) {

	for(var i = 0; i < slInput.length; i++) {
		//旕敿妏暥帤偺帪
		if (csStringSize(slInput.charAt(i)) == 2) {
			return false;
		}
	}

	return true;
}

function chkZenkakuString(slInput){

	for(var i = 0; i < slInput.length; i++) {
		if (csStringSize(slInput.charAt(i)) == 1) {
			return false;
		}
	}

	return true;
}

function chkRadio(olFrom,slRadioName){

	var alRadios = olFrom.all(slRadioName);
	var blResult = false;

	if(alRadios != undefined){
		for(var nlIndex = 0;nlIndex < alRadios.length; ++nlIndex){
			if(alRadios[nlIndex].checked){
				blResult = true;
				break;
			}
		}
	}

	return blResult;
}

function delCombItemBySelect(olItem){

	slValue = olItem.value;
	if(slValue == ""){
		return "1";
	}
	var olChildItems = olItem.childNodes;

	var nlIndex = 0

	for(nlIndex = 0 ; nlIndex < olChildItems.length; ++nlIndex) {
		if(olChildItems[nlIndex].value == slValue){
			olChildItems[nlIndex].removeNode(true);
			break;
		}
	}

	if(olChildItems.length == 1){
		olItem.value = olChildItems[0].value;
	} else if(olChildItems.length == 0){
	} else if(olChildItems.length > nlIndex){
		olItem.value = olChildItems[nlIndex].value;
	} else {
		olItem.value = olChildItems[nlIndex-1].value;
	}

	return "0";
}

function addCombItem(olItem,slValue,slName){

	var olChildItem = document.createElement("option");
	olChildItem.value = slValue;
	olChildItem.innerHTML = slName;
	olChildItem.selected = true;

	olItem.appendChild(olChildItem);
}

function getCombItemsByStr(olItem){

	var	olChildItems = olItem.childNodes;

	var alData = new Array();

	for(var nlIndex = 0 ; nlIndex < olChildItems.length; ++nlIndex){
		alData.push(olChildItems[nlIndex].value);
	}

	return alData.join(",");
}

var sgTabFlg = "";
function onShiftTab(){
	if(event.shiftKey && event.keyCode == 9){
		sgTabFlg = 'shiftab';
		document.getElementById("hidTabTxt").focus();
	}
}

function onEndTab(olItem){
	if(sgTabFlg == "shiftab"){
		sgTabFlg = "";
	} else {
		olItem.focus();
	}
}

var oPopupWin=new Array();

function closeSubPage(){
	var length=oPopupWin.length;
	for(var i=0;i<length;i++){
		if(!eval(oPopupWin[i]).closed){
			eval(oPopupWin[i]).close();
		}
	}
}

function addSubPage(swinId){
		var length=oPopupWin.length;
	for(var i=0;i<length;i++){
		if(oPopupWin[i] == swinId){
			return;
		}
	}
	oPopupWin[oPopupWin.length] = swinId;
}

var ogFocusItem = null;
var ogSelectItem = null;
function setFocus(olItem){
	if(olItem && olItem.focus){
		ogFocusItem = olItem;

		this.setTimeout("ogFocusItem.focus()",10);
	}
	if(olItem && olItem.tagName == "INPUT"  && olItem.type == "text" || 
		olItem && olItem.tagName == "TEXTAREA"){
		ogSelectItem = olItem;

		this.setTimeout("ogSelectItem.select()",11);
	}
}
function getAmount(olParams){

	var olRet = new Object();
	olRet.bodyPrice = 0;			
	olRet.taxPrice = 0;		
	olRet.taxCode = 0;		
	olRet.fractionsCode = 0;		
	olRet.totalPrice = 0;			
	olRet.returnFlg = "9";			
	olRet.errCode = "";				
	olRet.errMessage = "";				

	olRet.bodyPrice = Number(olParams.bodyPrice);
	if(isNaN(olRet.bodyPrice) || isNullOrSpace(olParams.bodyPrice)){
		olRet.returnFlg = "2";
		return olRet;
	}


	if(String(olParams.taxCode).match(/^\w\d\d\d$/g) == null){
		olRet.returnFlg = "2";
		return olRet;
	}

	if(isNullOrSpace(olParams.fractionsCode)){
		olRet.returnFlg = "2";
		return olRet;
	}

	if(isNullOrSpace(olParams.taxPrice)){

		var nlTaxRate = Number(olParams.taxCode.charAt(2));

		olRet.taxPrice = olRet.bodyPrice * (nlTaxRate / 100);

		switch (olParams.fractionsCode) {
			case "0" :		//0
				olRet.taxPrice = Math.round(olRet.taxPrice);
				break;
			case "1" :		//1
				olRet.taxPrice = Math.floor(olRet.taxPrice);
				break;
			case "2" :		//2
				olRet.taxPrice = Math.ceil(olRet.taxPrice);
				break;
		}
	} else {
		olRet.taxPrice = Number(olParams.taxPrice);
		if(isNaN(olRet.taxPrice)){
			olRet.taxPrice = 0;
		}
	}


	olRet.totalPrice = olRet.bodyPrice +  olRet.taxPrice;

	olRet.taxCode = olParams.taxCode;				
	olRet.fractionsCode = olParams.fractionsCode;		
	olRet.returnFlg = "0";							


	return olRet;
}

function isNullOrSpace(slStr){
	return (typeof(slStr) == "undefined" || slStr == null || String(slStr).replace(/^\s*|\s*$/g,"") == "");
}
function chkPhoneFaxNumeric(slInput){
	return slInput.match(/[^A-Za-z0-9|\-]/g) == null;
}

function setAmount(olParams){

	var olRet = new Object();
		olRet.returnFlg = "9";			
		olRet.errCode = "";				
		olRet.errMessage = "";			


	slBodyPrice = olParams.bodyItem.value.replace(/,/g,"").trim();		
	slOrgBodyPrice = olParams.orgBodyItem.value.replace(/,/g,"").trim();	
	slTaxPrice = olParams.taxItem.value.replace(/,/g,"").trim();		
	slOrgTaxPrice = olParams.orgTaxItem.value.replace(/,/g,"").trim();		

	if(slBodyPrice != ""){

		if(!olParams.bodyMin) olParams.bodyMin = 0;
		if(!olParams.bodyMax) olParams.bodyMax = 99999999999;

		if(!chkNumInter(slBodyPrice)){
			olRet.returnFlg = "1";
		}
		else if(!chkNumericRange(slBodyPrice,olParams.bodyMin,olParams.bodyMax)){
			olRet.returnFlg = "2";
		}


		if(olRet.returnFlg != "9"){
			if(olParams.action == "1"){
				return olRet;
			} else {
				olRet.returnFlg = "10";
				return olRet;
			}
		}
	}

	if(olParams.action == "3" && slBodyPrice == ""){
		olRet.returnFlg = "7";
		return olRet;
	}

	if(slTaxPrice != ""){
		if(!olParams.taxMin) olParams.taxMin = 0;
		if(!olParams.taxMax) olParams.taxMax = 99999999999;
		
		if(!chkNumInter(slTaxPrice)){
			olRet.returnFlg = "3";
		}
		else if(!chkNumericRange(slTaxPrice,olParams.taxMin,olParams.taxMax)){
			olRet.returnFlg = "4";
		}

		if(olRet.returnFlg != "9"){
			if(olParams.action == "2"){
				return olRet;
			} else {
				olRet.returnFlg = "10";
				return olRet;
			}
		}
	}
	if(!olParams.taxCode || olParams.taxCode == ""){
		olRet.returnFlg = "5";
		return olRet;
	}

	if(!olParams.fractionsCode || olParams.fractionsCode == ""){
		olRet.returnFlg = "6";
		return olRet;
	}

	var slRealTaxPrice = "";

	if(slBodyPrice != ""){

		var nlTaxRate = Number(olParams.taxCode.charAt(2));

		slRealTaxPrice = Number(slBodyPrice) * (nlTaxRate / 100);

		switch (olParams.fractionsCode) {
			case "0" :		//0:
				slRealTaxPrice = Math.round(slRealTaxPrice);
				break;
			case "1" :		//1:
				slRealTaxPrice = Math.floor(slRealTaxPrice);
				break;
			case "2" :		//2:
				slRealTaxPrice = Math.ceil(slRealTaxPrice);
				break;
			default : 		//0:
				slRealTaxPrice = Math.round(slRealTaxPrice);
				break;
		}


		slRealTaxPrice = String(slRealTaxPrice);
	}

	if(olParams.action == "1"){
		if(slBodyPrice != slOrgBodyPrice){
			slTaxPrice = slRealTaxPrice;
		}
	}
	else if(olParams.action == "2"){
		/*if(slTaxPrice != slOrgTaxPrice
			&& Math.abs((Number(slTaxPrice) - Number(slRealTaxPrice))) > Number(olParams.taxRange)
			&& !confirm(olParams.taxRangeOverMsg))*/
		if( slBodyPrice != "" 
			&&Math.abs((Number(slTaxPrice) - Number(slRealTaxPrice))) > Number(olParams.taxRange)
			&& !confirm(olParams.taxRangeOverMsg)){
			slTaxPrice = slRealTaxPrice;
		}
	}
	
	else {
		slTaxPrice = slRealTaxPrice;
	}

	var slSumPrice = "";
	if(slBodyPrice != "" || slTaxPrice != "") slSumPrice = String(Number(slBodyPrice) + Number(slTaxPrice));


	olActiveItem = document.activeElement;

	if(olActiveItem == olParams.bodyItem){
		olParams.bodyItem.value = slBodyPrice;
		setFocus(olActiveItem);
	} else {
		olParams.bodyItem.value = slBodyPrice != "" ? csNumFmt(slBodyPrice) : "";
	}

	if(olActiveItem == olParams.taxItem){
		olParams.taxItem.value = slTaxPrice;
		setFocus(olActiveItem);
	} else {
		olParams.taxItem.value = slTaxPrice != "" ? csNumFmt(slTaxPrice) : "";
	}

	olParams.sumItem.value = slSumPrice != "" ? csNumFmt(slSumPrice) : "";

	olParams.orgBodyItem.value = olParams.bodyItem.value;
	olParams.orgTaxItem.value = olParams.taxItem.value;
	olRet.returnFlg = "0";			
	olRet.errCode = "";					
	olRet.errMessage = "";				

	return olRet;
}
^_^.........分享是一种快乐!
 

你可能感兴趣的:(JavaScript,日期,trim,数字校验,键盘数字输入)