欢迎光临
我们一直在努力

.net中正则表达式的客户端验证–javascript-ASP教程,正则表达式

建站超值云服务器,限时71元/月

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

<form id=form1 method=post runat=server>
            
<font face=宋体></font><font face=宋体></font>
            
<br>
            
1. name :<br>
            
<asp:textbox id=txtname runat=server /><br>
            
2. email :<br>
            
<asp:textbox id=txtemail runat=server /><br>
            
3. web url :<br>
            
<asp:textbox id=txtweburl runat=server /><br>
            
4. zip :<br>
            
<asp:textbox id=txtzip runat=server /><br>
            
5.content<br>
            
<asp:textbox id=txtcontent runat=server textmode=multiline width=504px height=80px />
            
<br>
            
<asp:button id=btnsubmit onclientclick= return validate() runat=server text=submit />
        
</form>

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

<script language=javascript type=text/javascript>
                
function validate()
                        
{
                            
if (document.getelementbyid(<%=txtname.clientid%>).value==“”)
                            
{
                                        alert(
name feild can not be blank);
                                        document.getelementbyid(
<%=txtname.clientid%>).focus();
                                        
return false;
                            }

                            
if(document.getelementbyid(<%=txtemail.clientid %>).value==“”)
                            
{
                                        alert(
email id can not be blank);
                                        document.getelementbyid(
<%=txtemail.clientid %>).focus();
                                        
return false;
                            }

                            
// var emailpat = /^(“.*”|[a-za-z]w*)@([d{1,3}(.d{1,3}){3}]|[a-za-z]w*(.[a-za-z]w*)+)$/;
                            //var emailpat = “w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*”;
                            //var emailpat    =    /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
                            //var emailpat    =    ^([w]+@([w]+.)+[a-za-z]{2,9}(s*;s*[w]+@([w]+.)+[a-za-z]{2,9})*)$;
                            var emailpat    =    ^[a-za-z0-9_.]+@[a-za-z0-9-]+[.a-za-z]+$;
                            
var emailid=document.getelementbyid(<%=txtemail.clientid %>).value;
                            
var matcharray = emailid.match(emailpat);
                            
if (matcharray == null)
                            
{
                                    alert(
your email address seems incorrect. please try again.);
                                    document.getelementbyid(
<%=txtemail.clientid %>).focus();
                                    
return false;
                            }

                            
if(document.getelementbyid(<%=txtweburl.clientid %>).value==“”)
                            
{
                                    alert(
web url can not be blank);
                                    document.getelementbyid(
<%=txtweburl.clientid %>).value=http://
                                    document.getelementbyid(
<%=txtweburl.clientid %>).focus();
                                    
return false;
                            }

                            
var url=^[a-za-z]+://[a-za-z0-9-_]+\.[a-za-z0-9-_%&?/.=]+$
                            
var tempurl=document.getelementbyid(<%=txtweburl.clientid%>).value;
                            
var matchurl=tempurl.match(url);
                            
if(matchurl==null)
                            
{
                                    alert(
web url does not look valid);
                                    document.getelementbyid(
<%=txtweburl.clientid %>).focus();
                                    
return false;
                            }

                            
if (document.getelementbyid(<%=txtzip.clientid%>).value==“”)
                            
{
                                    alert(
zip code is not valid);
                                    document.getelementbyid(
<%=txtzip.clientid%>).focus();
                                    
return false;
                            }

                            
var digits=0123456789;
                            
var temp;
                            
for (var i=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(
please enter correct zip code);
                                                document.getelementbyid(
<%=txtzip.clientid%>).focus();
                                                
return false;
                                    }

                            }

                            
var contentlength=“”;
                            
if (document.getelementbyid(<%=txtcontent.clientid%>).value==“”)
                            
{
                                    alert(
content is need!);
                                    document.getelementbyid(
<%=txtcontent.clientid%>).focus();
                                    
return false;
                            }

                            
else if(document.getelementbyid(<%=txtcontent.clientid%>).value.length>100)
                            
{
                                alert(
content is too long,and more than 100 chars!);
                                    document.getelementbyid(
<%=txtcontent.clientid%>).focus();
                                    
return false;
                            }

                            
return true;
                        }

        
</script>

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

private void page_load(object sender, system.eventargs e)
        
{
             btnsubmit.attributes.add(
“onclick”, “return validate()”);
        }

 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » .net中正则表达式的客户端验证–javascript-ASP教程,正则表达式
分享到: 更多 (0)