'자바스크립트'에 해당되는 글 2건

  1. 2008/01/14 Simple Javascript Form Validator (1)
  2. 2008/01/02 window.onscrollend 이벤트 (3)

회사에서 사용중인 웹개발 프레임워크인 Lucy는 Webwork를 감싸서 사용하고 있는데, Validation Annotation 을 엉망으로 꼬아놓는 바람에 Webwork Client-Side Validation을 사용할 수 없고 촉박한 일정등의 상황때문에 간단한 Javascript form validator를 생각해보게 되었다.

Concept은 http://tetlaw.id.au/view/javascript/really-easy-field-validation 에서 따왔으나, alert 으로 알려주는 국내 방식(우리 회사 방식? 별로 맘에 들진 않는다.)에 맞지 않고, 검색 서비스 특성상 무거운 prototype.js 를 사용할 수도 없기 때문에 간단하게 만들어 보았다.

간단히 만들었으니 당연히 기능은 미약하기 짝이 없다.

validator.js

다운로드


사용방법은

  • 아래처럼, 자바스크립트 라이브러리를 포함하고,

    <script src="validator.js" type="text/javascript"></script>

  • 다음과 같이 form 요소를 작성한다

    <input class="required validate-number" id="field1" name="field1"  title="필드 1" />

    validation type을 class 속성을 통해 전달 한다.

  • 그 다음, 아래와 같이 자바스크립트를 초기화 하면 된다.

    	document.폼이름.onsubmit = function() {
    		return Validator.validate(this);
    	}
    

지원하는 validation type

  • required : 반드시 값을 입력해야 함
  • validate-number : 숫자만 입력가능 (., +, - 포함)
  • validate-digits : 자릿수만 입력가능 (숫자만)
  • validate-alpha : 알파뱃만 입력가능
  • validate-alphanum : 알파뱃과 숫자만 가능
  • validate-date : 날짜만 가능
  • validate-email : 이메일 주소
  • validate-url : URL

댓글을 달아 주세요

  1. iHWAN 2009/02/08 12:55  댓글주소  수정/삭제  댓글쓰기

    감사합니다. 잘 사용하겠습니다.

window.onscrollend 이벤트

2008/01/02 10:08

window에서 scroll의 끝에 다다랐을때의 이벤트가 필요해서 만들었다.
세로 스크롤을 끝까지 내리면 발생하는 Event.
(창, 스크롤의 크기를 확인하는 function은 lightbox.js 로 부터 가져왔다.)

예제보기

코드는 아래와 같다.

// creates "onscrollend" event
window.onscroll = function() {
	var scroll = getPageScroll(); // function from lightbox.js
	var size = getPageSize(); // function from lightbox.js

	if((size[3] + scroll[1])/size[1] == 1) {
		if(window.onscrollend) {
			return window.onscrollend();
		}
		return false;
	}
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

댓글을 달아 주세요

  1. 티눈제거 2008/01/02 13:40  댓글주소  수정/삭제  댓글쓰기

    어.... 잘했네
    예제는...익스플로러 버그인가? 알럿이 두번씩 뜨네?
    오페라에서는 무한 알럿!!! 파폭, 네비게텨, 사파리(윈용)만 정상동작

  2. 컴미ㆀ 2008/01/02 20:23  댓글주소  수정/삭제  댓글쓰기

    Good~!
    사진탭에서 상단에 페이지단위로 볼 수 있도록 추가해주면 좋겠다.(날짜별 검색등도??)

    • fguy 2008/01/17 17:11  댓글주소  수정/삭제

      Flickr 사이트(http://www.flickr.com/photos/fguy/)들어가서 보면 되삼.
      내 홈페이지에 있는 건 그냥 Preview