.net中正则表达式的客户端验证--javascript

本文介绍如何在ASP.NET应用中使用JavaScript进行客户端验证,包括必填项检查、电子邮件格式验证、网址有效性验证及邮政编码正确性校验等。

.net中客户端验证可以用微软自带的验证控件,但明显没有直接写的Javascript来得简单有效,请参照以下三步:
第一:Form如下:

<formid="Form1"method="post"runat="server">
<FONTface="宋体"></FONT><FONTface="宋体"></FONT>
<br>
1.Name:<br>
<asp:TextBoxID="txtName"runat="server"/><br>
2.Email:<br>
<asp:TextBoxID="txtEmail"runat="server"/><br>
3.WebURL:<br>
<asp:TextBoxID="txtWebURL"runat="server"/><br>
4.Zip:<br>
<asp:TextBoxID="txtZIP"runat="server"/><br>
5.Content<br>
<asp:TextBoxID="txtContent"runat="server"TextMode="MultiLine"Width="504px"Height="80px"/>
<br>
<asp:ButtonID="btnSubmit"OnClientClick="returnvalidate()"runat="server"Text="Submit"/>
</form>


第二:调用函数如下(可自行扩展)

<scriptlanguage="javascript"type="text/javascript">
functionvalidate()
...{
if(document.getElementById("<%=txtName.ClientID%>").value=="")
...{
alert(
"NameFeildcannotbeblank");
document.getElementById(
"<%=txtName.ClientID%>").focus();
returnfalse;
}

if(document.getElementById("<%=txtEmail.ClientID%>").value=="")
...{
alert(
"Emailidcannotbeblank");
document.getElementById(
"<%=txtEmail.ClientID%>").focus();
returnfalse;
}

//varemailPat=/^(".*"|[A-Za-z]w*)@([d{1,3}(.d{1,3}){3}]|[A-Za-z]w*(.[A-Za-z]w*)+)$/;
//varemailPat="w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*";
//varemailPat=/^[/w-]+(/.[/w-]+)*@[/w-]+(/.[/w-]+)+$/;
//varemailPat='^([w]+@([w]+.)+[a-zA-Z]{2,9}(s*;s*[w]+@([w]+.)+[a-zA-Z]{2,9})*)$';
varemailPat="^[a-zA-Z0-9_.]+@[a-zA-Z0-9-]+[.a-zA-Z]+$";
varemailid=document.getElementById("<%=txtEmail.ClientID%>").value;
varmatchArray=emailid.match(emailPat);
if(matchArray==null)
...{
alert(
"Youremailaddressseemsincorrect.Pleasetryagain.");
document.getElementById(
"<%=txtEmail.ClientID%>").focus();
returnfalse;
}

if(document.getElementById("<%=txtWebURL.ClientID%>").value=="")
...{
alert(
"WebURLcannotbeblank");
document.getElementById(
"<%=txtWebURL.ClientID%>").value="http://"
document.getElementById(
"<%=txtWebURL.ClientID%>").focus();
returnfalse;
}

varUrl="^[A-Za-z]+://[A-Za-z0-9-_]+/.[A-Za-z0-9-_%&?/.=]+$"
vartempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;
varmatchURL=tempURL.match(Url);
if(matchURL==null)
...{
alert(
"WebURLdoesnotlookvalid");
document.getElementById(
"<%=txtWebURL.ClientID%>").focus();
returnfalse;
}

if(document.getElementById("<%=txtZIP.ClientID%>").value=="")
...{
alert(
"ZipCodeisnotvalid");
document.getElementById(
"<%=txtZIP.ClientID%>").focus();
returnfalse;
}

vardigits="0123456789";
vartemp;
for(vari=0;i<document.getElementById("<%=txtZIP.ClientID%>").value.length;i++)
...{
temp
=document.getElementById("<%=txtZIP.ClientID%>").value.substring(i,i+1);
if(digits.indexOf(temp)==-1)
...{
alert(
"Pleaseentercorrectzipcode");
document.getElementById(
"<%=txtZIP.ClientID%>").focus();
returnfalse;
}

}

varContentLength="";
if(document.getElementById("<%=txtContent.ClientID%>").value=="")
...{
alert(
"Contentisneed!");
document.getElementById(
"<%=txtContent.ClientID%>").focus();
returnfalse;
}

elseif(document.getElementById("<%=txtContent.ClientID%>").value.length>100)
...{
alert(
"ContentisTooLong,andMorethan100Chars!");
document.getElementById(
"<%=txtContent.ClientID%>").focus();
returnfalse;
}

returntrue;
}

</script>



第三:Page_Load事件中加入一行:

privatevoidPage_Load(objectsender,System.EventArgse)
...{
btnSubmit.Attributes.Add(
"onclick","returnvalidate()");
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值