JQuery 플러그인 14번째: 간단한 양식 검증

9251 단어
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
	JQuery : 
</title>
<script type="text/javascript" language="javascript" src="jquery-1.6.js" ></script>
<script type="text/javascript" language="javascript">		
 jQuery.Validate=
{
    submit:function(pParam1)
    {
        var rtnValue=true,propertyCount=0,$e=(arguments.length==1)?$("#"+pParam1+" input"):$("input",document.forms[0]);
        $e.each(function(){
            if(rtnValue==false) return false;// 
            var e=this;
            switch(e.type.toLowerCase())
            {
                case 'text':
                   if(e.isnull=='false'){
                        if(e.value==''){
                            alert(e.alerttext+' ');
                            e.focus();
                            rtnValue=false;
                        }
                    }
                    if(rtnValue==true){
                        if(e.isnum=='true'){   
                                if(isNaN(e.value)){
                                    // 
                                    alert(e.alerttext+' !');
                                    e.select();
                                    e.focus();    
                                    rtnValue=false;
                                }
                                else{ // 
                                if(e.minvalue!=null){propertyCount+=1;}
                                if(e.maxvalue!=null){propertyCount+=1;}
                                if(propertyCount==2){
                                    if(parseFloat(e.value)<parseFloat(e.minvalue) || parseFloat(e.value)>parseFloat(e.maxvalue)){
                                        alert(e.alerttext+'  '+e.minvalue+'   '+e.maxvalue+'  ');
                                        e.select();
                                        e.focus();    
                                        rtnValue=false;
                                    }
                                }
                                else if(e.minvalue!=null){
                                    if(parseFloat(e.value)<parseFloat(e.minvalue)){
                                        alert(e.alerttext+'  '+e.minvalue);
                                        e.select();
                                        e.focus();    
                                        rtnValue=false;
                                    }
                                }
                                else if(e.maxvalue!=null){
                                    if(parseFloat(e.value)>parseFloat(e.maxvalue)){
                                        alert(e.alerttext+'  '+e.maxvalue);
                                        e.select();
                                        e.focus();    
                                        rtnValue=false;
                                    }
                                }
                            }
                        }
                    }
                    break;
            }
        });
        return rtnValue;
    },
    back:function()
    {
	    location.href = '?';
    }
}
$(function(){
    $("#btn").click(function(){
        jQuery.Validate.submit();
    });
    
    $("#go").click(function(){
        jQuery.Validate.back();
    });
    
});

    </script>
</head>
<body>
    <form name="form1" method="post" action="jq_1.aspx" id="form1">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTM5NDgwMzIxMmRkuoF+mEcyzpPvnvdc5QOQOKra/VI=" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


<div>

	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBALO1YyrBALd1vK2CgLs0bLrBgKSoqqWD4UI/Grs09yLYQGt8AokkTNdgoIy" />
</div>
        <div>
            <table width="85%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#C1CCDB"
                style="margin-top: 20px">
                <tr>
                    <td width="13%" height="28" align="center" bgcolor="#f4f7fa" class="shisidian">
                         </td>
                    <td width="87%" bgcolor="#f4f7fa" class="bule">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td width="2%">
                                     </td>
                                <td width="98%">
                                    <input name="CName" type="text" maxlength="50" id="CName" alerttext=" " isnull="false" style="width:330px;" /><span style="color: Red">*</span>( )</td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td width="13%" height="28" align="center" bgcolor="#f4f7fa" class="shisidian">
                        </td>
                    <td width="87%" bgcolor="#f4f7fa" class="bule">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td width="2%">
                                     </td>
                                <td width="98%">
                                    <input name="TextBox1" type="text" maxlength="50" id="TextBox1" alerttext=" " isnull="false" isnum="true" maxvalue="100000" minvalue="10" style="width:330px;" /><span style="color: Red">*</span>( )</td>
                            </tr>
                        </table>
                    </td>
                </tr>

            </table>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="38%" height="55" >
                         </td>
                    <td width="15%">
                        <a id="btn" href="javascript:__doPostBack('btn','')">
							<img src="../images/tianjia.gif" width="61" height="26" border="0" /></a>
                    </td>
                    <td width="47%">
                        <a id="go" href="#" >
                            <img src="../images/fanhui.gif" width="61" height="26" border="0" /></a>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>
주로 폼에 대한 검증 수요에 따라 사용자 정의 속성 isnull, isnum,maxvalue,minvalue 설정

좋은 웹페이지 즐겨찾기