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

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

None.gif < form id = " Form1 "  method = " post "  runat = " server " >
None.gif            
< FONT face = " 宋体 " ></ FONT >< FONT face = " 宋体 " ></ FONT >
None.gif            
< br >
None.gif            
1 . Name : < br >
None.gif            
< asp:TextBox ID = " txtName "  runat = " server "   />< br >
None.gif            
2 . Email : < br >
None.gif            
< asp:TextBox ID = " txtEmail "  runat = " server "   />< br >
None.gif            
3 . Web URL : < br >
None.gif            
< asp:TextBox ID = " txtWebURL "  runat = " server "   />< br >
None.gif            
4 . Zip : < br >
None.gif            
< asp:TextBox ID = " txtZIP "  runat = " server "   />< br >
None.gif            
5 .Content < br >
None.gif            
< asp:TextBox ID = " txtContent "  runat = " server "  TextMode = " MultiLine "  Width = " 504px "  Height = " 80px "   />
None.gif            
< br >
None.gif            
< asp:Button ID = " btnSubmit "  OnClientClick = "  return validate() "  runat = " server "  Text = " Submit "   />
None.gif        
</ form >


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

None.gif < script language = " javascript "  type = " text/javascript " >
None.gif                
function  validate()
ExpandedBlockStart.gifContractedBlock.gif                        
... {
InBlock.gif                            
if (document.getElementById("<%=txtName.ClientID%>").value=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
...{
InBlock.gif                                        alert(
"Name Feild can not be blank");
InBlock.gif                                        document.getElementById(
"<%=txtName.ClientID%>").focus();
InBlock.gif                                        
return false;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
if(document.getElementById("<%=txtEmail.ClientID %>").value=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
...{
InBlock.gif                                        alert(
"Email id can not be blank");
InBlock.gif                                        document.getElementById(
"<%=txtEmail.ClientID %>").focus();
InBlock.gif                                        
return false;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
// var emailPat = /^(".*"|[A-Za-z]w*)@([d{1,3}(.d{1,3}){3}]|[A-Za-z]w*(.[A-Za-z]w*)+)$/;
InBlock.gif
                            //var emailPat = "w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*";
InBlock.gif
                            //var emailPat    =    /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
InBlock.gif
                            //var emailPat    =    '^([w]+@([w]+.)+[a-zA-Z]{2,9}(s*;s*[w]+@([w]+.)+[a-zA-Z]{2,9})*)$';
InBlock.gif
                            var emailPat    =    "^[a-zA-Z0-9_.]+@[a-zA-Z0-9-]+[.a-zA-Z]+$";
InBlock.gif                            
var emailid=document.getElementById("<%=txtEmail.ClientID %>").value;
InBlock.gif                            
var matchArray = emailid.match(emailPat);
InBlock.gif                            
if (matchArray == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
...{
InBlock.gif                                    alert(
"Your email address seems incorrect. Please try again.");
InBlock.gif                                    document.getElementById(
"<%=txtEmail.ClientID %>").focus();
InBlock.gif                                    
return false;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
if(document.getElementById("<%=txtWebURL.ClientID %>").value=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
...{
InBlock.gif                                    alert(
"Web URL can not be blank");
InBlock.gif                                    document.getElementById(
"<%=txtWebURL.ClientID %>").value="http://"
InBlock.gif                                    document.getElementById(
"<%=txtWebURL.ClientID %>").focus();
InBlock.gif                                    
return false;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\.[A-Za-z0-9-_%&?/.=]+$"
InBlock.gif                            
var tempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;
InBlock.gif                            
var matchURL=tempURL.match(Url);
InBlock.gif                            
if(matchURL==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
...{
InBlock.gif                                    alert(
"Web URL does not look valid");
InBlock.gif                                    document.getElementById(
"<%=txtWebURL.ClientID %>").focus();
InBlock.gif                                    
return false;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
if (document.getElementById("<%=txtZIP.ClientID%>").value=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
...{
InBlock.gif                                    alert(
"Zip Code is not valid");
InBlock.gif                                    document.getElementById(
"<%=txtZIP.ClientID%>").focus();
InBlock.gif                                    
return false;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
var digits="0123456789";
InBlock.gif                            
var temp;
InBlock.gif                            
for (var i=0;i<document.getElementById("<%=txtZIP.ClientID %>").value.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
...{
InBlock.gif                                    temp
=document.getElementById("<%=txtZIP.ClientID%>").value.substring(i,i+1);
InBlock.gif                                    
if (digits.indexOf(temp)==-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
...{
InBlock.gif                                                alert(
"Please enter correct zip code");
InBlock.gif                                                document.getElementById(
"<%=txtZIP.ClientID%>").focus();
InBlock.gif                                                
return false;
ExpandedSubBlockEnd.gif                                    }

ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
var ContentLength="";
InBlock.gif                            
if (document.getElementById("<%=txtContent.ClientID%>").value=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
...{
InBlock.gif                                    alert(
"Content is need!");
InBlock.gif                                    document.getElementById(
"<%=txtContent.ClientID%>").focus();
InBlock.gif                                    
return false;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
else if(document.getElementById("<%=txtContent.ClientID%>").value.length>100)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
...{
InBlock.gif                                alert(
"Content is Too Long,and More than 100 Chars!");
InBlock.gif                                    document.getElementById(
"<%=txtContent.ClientID%>").focus();
InBlock.gif                                    
return false;
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
return true;
ExpandedBlockEnd.gif                        }

None.gif        
</ script >



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

None.gif private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
... {
InBlock.gif             btnSubmit.Attributes.Add(
"onclick", "return validate()");
ExpandedBlockEnd.gif        }

None.gif




 

转载于:https://www.cnblogs.com/downmoon/archive/2007/12/29/1019875.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值