기본 콘텐츠로 건너뛰기

[javascript] Form validation 체크 - validate.js


Form에서 필드값들 유효성 체크할때 괜찮은 javascript 있다. 



validate.js



설치 방법
Include the JavaScript file in your source
<script type="text/javascript" src="validate.min.js"></script>
사용법
Create the validation object with your desired rules. This needs to be in a <script> tag located just before your closing </body> tag. The reason for this being that the DOM needs to have your form loaded before you can attach your rules.
var validator = new FormValidator('example_form', [{
    name: 'req',
    display: 'required',    
    rules: 'required'
}, {
    name: 'alphanumeric',
    rules: 'alpha_numeric'
}, {
    name: 'password',
    rules: 'required'
}, {
    name: 'password_confirm',
    display: 'password confirmation',
    rules: 'required|matches[password]'
}, {
    name: 'email',
    rules: 'valid_email'
}, {
    name: 'minlength',
    display: 'min length',
    rules: 'min_length[8]'
}], function(errors, event) {
    if (errors.length > 0) {
        // Show the errors
    }
});
'examlpe_form'은 해당 Form의 이름이고, 
각 필드마다 중괄호를 열어서 적어주면 된다. 

  name: '필드name',
  rules: '해당필드에 적합한 규칙'  
  display : 'placeholder'


필수값, 최대크키, 숫자 혹은 문자만, 이메일주소 유효성, ip, url 등
어지간한건 전부 있다.
RuleDescriptionParameterExample
requiredreturns false if the form element is empty.no
matchesreturns false if the form element value does not match the one in the parameter.yesmatches[other_element]
valid_emailreturns false if the form element value is not a valid email address.no
valid_emailsreturns false if any value provided in a comma separated list is not a valid email.no
min_lengthreturns false if the form element value is shorter than the parameter.yesmin_length[6]
max_lengthreturns false if the form element value is longer than the parameter.yesmax_length[8]
exact_lengthreturns false if the form element value length is not exactly the parameter.yesexact_length[4]
greater_thanreturns false if the form element value is less than the parameter after using parseFloat.yesgreater_than[10]
less_thanreturns false if the form element value is greater than the parameter after using parseFloat.yesless_than[2]
alphareturns false if the form element contains anything other than alphabetical characters.no
alpha_numericreturns false if the form element contains anything other than alpha-numeric characters.no
alpha_dashreturns false if the form element contains anything other than alphanumeric characters, underscores, or dashes.no
numericreturns false if the form element contains anything other than numeric characters.no
integerreturns false if the form element contains anything other than an integer.no
decimalreturns false if the form element contains anything other than a decimal.no
is_naturalreturns false if the form element contains anything other than a natural number: 0, 1, 2, 3, etc.no
is_natural_no_zeroreturns false if the form element contains anything other than a natural number, but not zero: 1, 2, 3, etc.no
valid_ipreturns false if the supplied IP is not valid.no
valid_base64returns false if the supplied string contains anything other than valid Base64 characters.no
valid_credit_cardreturns false if the supplied string is not a valid credit cardno
valid_urlreturns false if the supplied string is not a valid urlno
is_file_typereturns false if the supplied file is not part of the comma separated list in the paramteryesis_file_type[gif,png,jpg]










댓글

이 블로그의 인기 게시물

메일서버가 스팸으로 취급받을때

설치한 메일서버를 통해 발송되는 메일이 스팸으로 들어가는 경우가 더러 있다. 이게 한번 들어가기는 쉬운데, 빠져나오기는 드럽게 힘든것 같다... 본인의 경우에는 우선 국내서비스에는 별 무리 없이 들어간다. (naver,daum 등) 그런데 해외메일 그중 Gmail, Hotmail 에는 에누리없이 스팸으로 간주되고 있었다. Gmail같은 경우에는 그래도 스팸함으로 발송은 제대로 되는반면에 Hotmail같은경우에는 아예 수신자체가 안되는 경우도 더러있다.. ㅡ,.ㅡ; 제일 좋은 방법은 Gmail,Hotmail에 전화걸어서 우리 메일서버 IP white Ip로 등록해달라!!! 하면 좋지만, 얘네들은 걸어봤자 자동응답기고, 문의채널은 구글 그룹스 게시판이 전부다.. 본론으로 들어가서. 해외 메일이 차단될 경우 내 매일서버ip가 스팸ip로 등록되 버린 경우일 수 있다. (본인처럼. ㅎ) 이것부터 조회 해보고 싶으면 RBL(real-time blocking List) 체크를 해야 하는데, RBL체크 해주는 사이트는 꽤 많이 있고, 그중 좀 깔끔해 보이는곳 하나 소개. http://www.anti-abuse.org/ 메일서버ip 입력하고 조회해보면 쭈루룩 리스트가 나온다. 그 중 빨간불이 들어온 부분이 메일 서버가 스팸서버가 된 각종 이유들이다.ㅋ 본인의 경우 CBL 때문에 걸렸는데, 내용은 아래와 같다. This IP address is HELO'ing as  "localhost.localdomain"  which violates the relevant standards (specifically: RFC5321). 메일서버 도메인에 별다른 작업을 안해놓아서 "localhost.localdomain" 으로 설정되어있었다. 만약 CBL만 바로 테스트 해보고 싶으면 http://cbl.abusea

[javascript] 특정시간에만 함수 실행

특정시간에만 팝업을 띄우려면?? 특정시간에만 로그인을 막으려면?? 특정시간에만 할일은 의외로 참 많다. 방법? 딱히 없다. 현재시간 구해서 시작시간, 종료시간 사이에 있을때 시작하는 수밖엔. if ((현재시간 > 시작시간) && (현재시간 < 종료시간)){ .. 팝업노출(); 공사페이지 리다이렉트(); 기타등등(); .. } 자바스크립트로 작성하면 다음과 같다. var startdate = "2014012008" ; var enddate = "2014012418" ; var now = new Date (); //현재시간 year = now. getFullYear (); //현재시간 중 4자리 연도 month = now. getMonth () + 1 ; //현재시간 중 달. 달은 0부터 시작하기 때문에 +1 if ((month + "" ). length < 2 ){ month = "0" + month; //달의 숫자가 1자리면 앞에 0을 붙임. } date = now. getDate (); //현재 시간 중 날짜. if ((date + "" ). length < 2 ){ date = "0" + date; } hour = now. getHours (); //현재 시간 중 시간. if ((hour + "" ). length < 2 ){ hour = "0" + hour; } today = year + "" + month + "" + date + "" + hour; //오늘 날짜 완성. / / 시간비교 i

스레드 동기화1 - syncronized

구현 스레드를 구현하는 방법은 2가지다. 1. Thread 클래스를 extends 한다. 2. Runnable 인터페이스를 implements 한다. 뭐 사실 Thread는 생성자의 변수로 Runnable을 취한다. public Thread (Runnable target) { init( null, target , "Thread-" + nextThreadNum () , 0 ) ; } 그리고 Runnable 인터페이스는 run() 이라는 단일함수를 갖는 인터페이스이다. @FunctionalInterface public interface Runnable { /** * When an object implementing interface <code> Runnable </code> is used * to create a thread, starting the thread causes the object's * <code> run </code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code> run </code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */ public abstract void run () ; } 그렇기 때문에 람다식으로 표현이 가능한 것이다. 동기화 동기화는 동시에 같은자원의 접근을 제한하고자 할때 사용한다. 예를들어, 한 우물에서 물을 15번 길어야 하는 일을 해야할때 5명이서 3번만 하면 수고를 5배로 줄일수 있다. 그런데 우물은 하난데 동시에