01 로그인....

프로그래밍/Web 2007. 11. 28. 09:06 Posted by galad

★ 이벤트 발생 시 처리할 때 return 값을 true로 주면 기본적인 처리를 마저 하고,

 false로 주면 하지 않는다.


ex) onSubmit="checkIDandPW(); return false"

submit 시 checkIDandPW() 함수를 실행 시키고 나서, 기본적인 처리 - 여기서는 submit - 를 하지 않는다.



<!--
/******************************************************************************
*   파일      : login.html
*   용도      : login 창 체크하는 자바 스크립트 예제
*   작성자   : 성홍제
*   작성일   : 2006. 08. 31
*   Version : 1.0
******************************************************************************/
 -->
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
    <title>로그인</title>
    <script type="text/javascript">
    <!--
    function checkIDandPW()
    {

        // var f = document.loginForm;

        // 이렇게 한번만 선언하고, 밑의 document.loginForm를 f라고 써도 된다.


        // form에 이름을 주지 않았을 경우에는 - 서버에서 동적으로 폼을 생성하는 경우에 쓴다

        // var f = document.forms[0];  // 0 번째 폼

        // var f = document.forms["loginForm"];  // 이름이 loginForm인 폼


        // ID체크
        if(document.loginForm.id.value == "")
        {
            alert("ID를 입력해주세요");
            document.loginForm.id.focus();
            return false;
        }
       
        // PW 체크
        if(document.loginForm.pw.value == "")
        {
            alert("PW를 입력해주세요");
            document.loginForm.pw.focus();
            return false;

        }
       
        alert("OK");
        return true;
    }
    //-->
    </script>
</head>
<body>
<form method="post" name="loginForm" action="" onSubmit="return checkIDandPW();" autocomplete="off">
    ID: <input type="text" name="id"><br>
    PW: <input type="text" name="pw"><br>
    <input type="submit" value="전송">
    <input type="reset" value="취소">
</form>
</body>
</html>


onSubmit 에서 checkIDandPW()의 리턴값을 리턴한다.

즉, 체크에 성공하면 true, 실패하면 false를 리턴하므로

성공시에는 기본처리인 submit을 하고, 실패하면 하지 않는다.