Skip to content

CHICPRO

  • Life Log
  • Cycling Log
  • Photo Log
    • Portrait
    • Landscape
    • Flower
    • Etc
  • Coding Log
  • Information

jQuery plugin: Validation

2011-11-22 by 편리

한창 작업 중인 쇼핑몰 사이트에 사용하기 위해 jQuery Validation 플러그인을 찾아보니 다행히 마음에 드는 강력한
플러그인이 있어 소개하고자 한다. 매번 직접 스크립트로 폼의 데이터를 검증했는데 이제는 좀 편하게할 수 있겠다.

Validation 플러그인 공식사이트 : http://bassistance.de/jquery-plugins/jquery-plugin-validation/
Validation 플러그인 참고 문서 : http://rocketsquared.com/wiki/Plugins/Validation

아래는 어떤 식으로 사용이 되는지를 보여주는 간단한 예제이다.
참고 예제 : http://jquery.bassistance.de/validate/demo/milk/

<script id=”demo” type=”text/javascript”>
$(document).ready(function() {
    // validate signup form on keyup and submit
    var validator = $(“#signupform”).validate({
        rules: {
            firstname: “required”,
            lastname: “required”,
            username: {
                required: true,
                minlength: 2,
                remote: “users.php”
            },
            password: {
                required: true,
                minlength: 5
            },
            password_confirm: {
                required: true,
                minlength: 5,
                equalTo: “#password”
            },
            email: {
                required: true,
                email: true,
                remote: “emails.php”
            },
            dateformat: “required”,
            terms: “required”
        },
        messages: {
            firstname: “Enter your firstname”,
            lastname: “Enter your lastname”,
            username: {
                required: “Enter a username”,
                minlength: jQuery.format(“Enter at least {0} characters”),
                remote: jQuery.format(“{0} is already in use”)
            },
            password: {
                required: “Provide a password”,
                rangelength: jQuery.format(“Enter at least {0} characters”)
            },
            password_confirm: {
                required: “Repeat your password”,
                minlength: jQuery.format(“Enter at least {0} characters”),
                equalTo: “Enter the same password as above”
            },
            email: {
                required: “Please enter a valid email address”,
                minlength: “Please enter a valid email address”,
                remote: jQuery.format(“{0} is already in use”)
            },
            dateformat: “Choose your preferred dateformat”,
            terms: ” “
        },
        // the errorPlacement has to take the table layout into account
        errorPlacement: function(error, element) {
            if ( element.is(“:radio”) )
                error.appendTo( element.parent().next().next() );
            else if ( element.is(“:checkbox”) )
                error.appendTo ( element.next() );
            else
                error.appendTo( element.parent().next() );
        },
        // specifying a submitHandler prevents the default submit, good for the demo
        submitHandler: function() {
            alert(“submitted!”);
        },
        // set this class to error-labels to indicate valid fields
        success: function(label) {
            // set &nbsp; as text for IE
            label.html(“&nbsp;”).addClass(“checked”);
        }
    });
   
    // propose username by combining first- and lastname
    $(“#username”).focus(function() {
        var firstname = $(“#firstname”).val();
        var lastname = $(“#lastname”).val();
        if(firstname && lastname && !this.value) {
            this.value = firstname + “.” + lastname;
        }
    });

});
</script>

<form id=”signupform” autocomplete=”off” method=”get” action=””>
  <table>
  <tr>
    <td class=”label”><label id=”lfirstname” for=”firstname”>First Name</label></td>
    <td class=”field”><input id=”firstname” name=”firstname” type=”text” value=”” maxlength=”100″ /></td>
    <td class=”status”></td>
  </tr>

  <tr>
    <td class=”label”><label id=”llastname” for=”lastname”>Last Name</label></td>
    <td class=”field”><input id=”lastname” name=”lastname” type=”text” value=”” maxlength=”100″ /></td>
    <td class=”status”></td>
  </tr>
  <tr>
    <td class=”label”><label id=”lusername” for=”username”>Username</label></td>
    <td class=”field”><input id=”username” name=”username” type=”text” value=”” maxlength=”50″ /></td>

    <td class=”status”></td>
  </tr>
  <tr>
    <td class=”label”><label id=”lpassword” for=”password”>Password</label></td>
    <td class=”field”><input id=”password” name=”password” type=”password” maxlength=”50″ value=”” /></td>
    <td class=”status”></td>
  </tr>
  <tr>

    <td class=”label”><label id=”lpassword_confirm” for=”password_confirm”>Confirm Password</label></td>
    <td class=”field”><input id=”password_confirm” name=”password_confirm” type=”password” maxlength=”50″ value=”” /></td>
    <td class=”status”></td>
  </tr>
  <tr>
    <td class=”label”><label id=”lemail” for=”email”>Email Address</label></td>
    <td class=”field”><input id=”email” name=”email” type=”text” value=”” maxlength=”150″ /></td>
    <td class=”status”></td>

  </tr>
              <tr>
    <td class=”label”><label>Which Looks Right</label></td>
    <td class=”field” colspan=”2″ style=”vertical-align: top; padding-top: 2px;”>
    <table>
    <tbody>

    <tr>
        <td style=”padding-right: 5px;”>

            <input id=”dateformat_eu” name=”dateformat” type=”radio” value=”0″ />
            <label id=”ldateformat_eu” for=”dateformat_eu”>14/02/07</label>
        </td>
        <td style=”padding-left: 5px;”>
            <input id=”dateformat_am” name=”dateformat” type=”radio” value=”1″  />
            <label id=”ldateformat_am” for=”dateformat_am”>02/14/07</label>
        </td>
        <td>

        </td>
    </tr>
    </tbody>
    </table>
    </td>
  </tr>

  <tr>
    <td class=”label”>&nbsp;</td>

    <td class=”field” colspan=”2″>
        <div id=”termswrap”>
            <input id=”terms” type=”checkbox” name=”terms” />
            <label id=”lterms” for=”terms”>I have read and accept the Terms of Use.</label>
        </div> <!– /termswrap –>
    </td>
  </tr>
  <tr>

    <td class=”label”><label id=”lsignupsubmit” for=”signupsubmit”>Signup</label></td>
    <td class=”field” colspan=”2″>
    <input id=”signupsubmit” name=”signup” type=”submit” value=”Signup” />
    </td>
  </tr>

  </table>
</form>

Validation시 각 필드에 적용된 규칙과 에러메시지도 직접 지정할 수 있어서 상당히 유용하게 사용될 것 같다.

Post navigation

Previous Post:

Desire 배터리 사용시간을 늘리기 위한 노력?

Next Post:

거참.. 일하기 참 힘드네~

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • SK 세븐모바일 유심 셀프교체
  • php 배열 연산에서 + 와 array_merge 의 차이
  • pcntl_fork 를 이용한 다중 프로세스 실행
  • 아이폰 단축어를 이용하여 주중 공휴일엔 알람 울리지 않게 하기
  • 구글 캘린더 전체일정 재동기화
  • OpenLiteSpeed 웹서버에 HTTP 인증 적용
  • OpenLiteSpeed 웹어드민 도메인 연결
  • WireGuard를 이용한 VPN 환경 구축
  • Ubuntu 22.04 서버에 OpenLiteSpeed 웹서버 세팅
  • 맥 vim 세팅

Recent Comments

  • 편리 on 업무관리용 그누보드 게시판 스킨
  • 임종섭 on 업무관리용 그누보드 게시판 스킨
  • 캐논 5D 펌웨어 | Dslr 펌웨어 업그레이드 방법 82 개의 베스트 답변 on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • Top 5 캐논 5D 펌웨어 Top 89 Best Answers on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • 편리 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 임팀장 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 편리 on Notepad++ NppFTP 플러그인 수동 설치
  • paul-j on Notepad++ NppFTP 플러그인 수동 설치
  • YS on Windows 10 iCloud 사진 저장 폴더 변경
  • 편리 on Docker를 이용한 Centos7 + httpd + php 5.4 개발환경 구축

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2025 CHICPRO | Built using WordPress and SuperbThemes