Blog Content

    티스토리 뷰

    32.C# ASP.NET - CompareValidator [ 유효성검사컨트롤 ]

    반응형
    CompareValidator : 비교

    FrmCompareValidator.aspx

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

        <title></title>

    </head>

    <body>

        <form id="form1" runat="server">

        <div>   

            비교<br /><br />

           

            암호 :

            <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>

           

            <asp:CompareValidator ID="valPassword" runat="server"

                ControlToValidate="txtPassword"

                ControlToCompare="txtPasswordConfirm"

                Type="String"

                ErrorMessage="암호를 확인하세요."></asp:CompareValidator><br />

               

            암호확인 :

            <asp:TextBox ID="txtPasswordConfirm" runat="server"></asp:TextBox>

            <br /><br />

           

            <asp:LinkButton ID="btnLogin" runat="server">로그인</asp:LinkButton>   

        </div>

        </form>

    </body>

    </html>


    결과화면

    [그림32-1]


    CompareValidator 컨트롤을 자바스크립트로 표현한 예제입니다.

    FrmCompareValidator.htm

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

        <title></title>

        <script type="text/javascript">

            function Ok() {

                if (document.getElementById("txtPassword").value !=

                document.getElementById("txtPasswordConfirm").value) {

                    alert("암호가 틀립니다.");

                   

                    document.getElementById("txtPassword").select(); // 선택

                }

                else {

                    alert("유효성 검사 통과");

                }

            }

        </script>

    </head>

    <body>

            비교 <br />

           

            암호 : <input type="text" id="txtPassword" name="txtPassword" /><br />

           

            암호확인 : <input type="text" id="txtPasswordConfirm"

                name="txtPasswordConfirm" /><br />

           

            <input type="button" value="확인" onclick="Ok();" />

    </body>

    </html>


    결과화면

    [그림32-2]



    반응형

    Comments