`
netxdiy
  • 浏览: 682751 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

.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()");
}





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics