asp.net实现中英文多域名检测的方法
本文实例讲述了asp.net实现中英文多域名检测的方法。分享给大家供大家参考,具体如下:
第一步:在前台页面中写入js代码和相关控件
/****写入js代码****/ <%--域名检测--%> <scripttype="text/javascript"> //判断输入的是否为中文域名 functionIsChDomain(domainStr) { varflag=false; varcompStr="ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_0123456789"; vardomainLength=domainStr.length;//判断字符长度 for(vari=0;i<domainLength;i++) { vartemp=domainStr.charCodeAt(i); if(temp>=10000)//含有字符编码大于10000的字符判断为中文,不太严格 { flag=true; } else { vartemp2=compStr.indexOf(domainStr.charAt(i)); if(temp2==-1) { flag=false; break; } } } returnflag; } //判断输入的是否为英文域名 functionIsEnDomain(domainStr) { varflag=false; varcompStr="ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_0123456789"; vardomainLength=domainStr.length; for(vari=0;i<domainLength;i++) { if(compStr.indexOf(domainStr.charAt(i))==-1) { flag=false; break; } else { flag=true; } } returnflag; } //中国等不能注册英文域名,把相应的CheckBox的disabled属性设置为true, functionTrim(domainStr) { returndomainStr.replace(/(^s*)|(s*$)/g,""); } //验证域名是哪一类型的 functioncheckDomainType() { vardomainName=document.getElementById("txtDomainName").value; domainName=Trim(domainName);//去掉输入的特殊符号 if(IsChDomain(domainName))//调用中文域名----------验证方法 { setCheckBox(true); } elseif(IsEnDomain(domainName))//调用英文域名-----验证方法 { setCheckBox(false); } } //为CheckBox复选框的Checked属性赋值 functionsetCheckBox(flag) { document.getElementById("chkcom").disabled=flag; document.getElementById("chknet").disabled=flag; document.getElementById("chkorg").disabled=flag; document.getElementById("chkorgcn").disabled=flag; document.getElementById("chkcn").disabled=flag; document.getElementById("chkcomcn").disabled=flag; document.getElementById("chknetcn").disabled=flag; document.getElementById("chkgovcn").disabled=flag; document.getElementById("chkcouk").disabled=flag; document.getElementById("chkbiz").disabled=flag; document.getElementById("chkcc").disabled=flag; document.getElementById("chktv").disabled=flag; document.getElementById("chkinfo").disabled=flag; document.getElementById("chkchina").disabled=!flag; document.getElementById("chkcompany").disabled=!flag; document.getElementById("chknetwork").disabled=!flag; document.getElementById("chkorguk").disabled=flag; document.getElementById("chkus").disabled=flag; document.getElementById("chkmeuk").disabled=flag; document.getElementById("chkltduk").disabled=flag; } //检查输入的字符规范 //functioncheckValue() //{ //if(document.getElementById("txtDomainName").value=='') //{ //alert('请输入域名!'); //returnfalse; //} //if(document.getElementById("txtDomainName").value.length>=60) //{ //alert('域名长度不能超过60个字符!'); //returnfalse; //} //for(vari=0;i<document.getElementById("txtDomainName").value.length;i++) //{ //if(document.getElementById("txtDomainName").value.charAt(i)=='') //{ //alert('域名中不能含有空格!'); //returnfalse; //break; //} //} //} </script> /***写入相关控件***/ //用于显示查询的结果 <asp:PanelID="indexpnlDomainName"runat="server"Width="100%"/> // <tablewidth="373"border="0"cellpadding="0"cellspacing="1"> <tr> <tdwidth="74"height="41"> <asp:CheckBoxID="chkcom"Text=".com"runat="server"/> </td> <tdwidth="71"height="41"> <asp:CheckBoxID="chkcn"Text=".cn"runat="server"/> </td> <tdwidth="79"height="41"> <asp:CheckBoxID="chkorg"Text=".org"runat="server"/> </td> <tdwidth="71"height="41"> <asp:CheckBoxID="chknet"Text=".net"runat="server"/> </td> <tdwidth="72"height="41"> <asp:CheckBoxID="chkinfo"Text=".info"runat="server"/> </td> </tr> <tr> <tdwidth="74"height="41"> <asp:CheckBoxID="chkcomcn"Text=".com.cn"runat="server"/> </td> <tdheight="41"> <asp:CheckBoxID="chkcc"Text=".cc"runat="server"/> </td> <tdheight="41"> <asp:CheckBoxID="chkorgcn"Text=".org.cn"runat="server"/> </td> <tdheight="41"> <asp:CheckBoxID="chknetcn"Text=".net.cn"runat="server"/> </td> <tdheight="41"> <asp:CheckBoxID="chkin"Text=".in"runat="server"/> </td> </tr> <tr> <tdwidth="74"height="40"> <asp:CheckBoxID="chkcouk"Text=".co.uk"runat="server"/> </td> <tdheight="41"> <asp:CheckBoxID="chktv"Text=".tv"runat="server"/> </td> <tdheight="41"> <asp:CheckBoxID="chkorguk"Text=".org.uk"runat="server"/> </td> <tdheight="41"> <asp:CheckBoxID="chkus"Text=".us"runat="server"/> </td> <tdheight="41"> <asp:CheckBoxID="chkmeuk"Text=".me.uk"runat="server"/> </td> </tr> <tr> <tdwidth="74"height="41"> <asp:CheckBoxID="chkltduk"Text=".ltd.uk"runat="server"/> </td> <tdheight="41"> <asp:CheckBoxID="chkbiz"Text=".biz"runat="server"/> </td> <tdheight="41"> <asp:CheckBoxID="chknetwork"Text=".网络"runat="server"/> </td> <tdheight="41"> <asp:CheckBoxID="chkcompany"Text=".公司"runat="server"/> </td> <tdheight="41"> <asp:CheckBoxID="chkchina"Text=".中国"runat="server"/> </td> </tr> </table>
第二步:在后台页面中写入方法
usingSystem; usingSystem.Collections; usingSystem.Configuration; usingSystem.Data; usingSystem.Linq; usingSystem.Web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI.HtmlControls; usingSystem.Web.UI.WebControls; usingSystem.Web.UI.WebControls.WebParts; usingSystem.Xml.Linq; usingSystem.Text;//(需要引入的类文件命名空间) usingSystem.Net.Sockets;//(需要引入的类文件命名空间) usingSystem.Text.RegularExpressions;//(需要引入的类文件命名空间) usingSystem.Collections.Generic;//(需要引入的类文件命名空间) usingSystem.Data.Common;//(需要引入的类文件命名空间) usingSystem.Xml;//(需要引入的类文件命名空间) usingSystem.IO;//(需要引入的类文件命名空间) usingMicrosoft.SqlServer.Server;//(需要引入的类文件命名空间) usingSystem.Net;//(需要引入的类文件命名空间) //判断是否为中文域名(方法) publicstaticboolIsHasCHZN(stringdomainName) { RegexRegCHZN=newRegex("[一-龥]"); Matchm=RegCHZN.Match(domainName); returnm.Success; } //判断域名是否被注册(方法) publicstaticboolIsReg(stringdomainName) { boolflag=false; stringdm=HttpContext.Current.Server.UrlEncode(domainName); try { //判断方法非常多,如打开远程文件再处理字符串等等,这里用的方法效率不是很高 WebClientwc=newWebClient(); stringxmlstr=wc.DownloadString("http://panda.www.net.cn/cgi-bin/check.cgi?area_domain="+dm); StringReadersr=newStringReader(xmlstr); XmlTextReaderxr=newXmlTextReader(sr); while(xr.Read()) { if(xr.IsStartElement("original")) { xr.Read(); if(xr.Value.Substring(0,3)=="210") { flag=true; break; } else { flag=false; break; } } } returnflag; } catch { returnfalse; } } //按钮事件中 protectedvoidimgBtnCheck_Click(objectsender,ImageClickEventArgse) { stringtxtYM=this.txtYMname.Text.Trim(); if(txtYM=="") {ScriptManager.RegisterStartupScript(this,this.GetType(),"sc","alert('对不起,域名不能为空!');",true);} else { stringdomainName=this.txtYMname.Text.Trim(); IList<string>domainList=newList<string>();//保存域名名称和后缀 domainList.Add(domainName);//List<string>第一元素保存域名名称,其他元素为域名后缀 //判断是否为中文域名 if(IsHasCHZN(domainName)) { if(chkchina.Checked)domainList.Add(chkchina.Text); if(chkcompany.Checked)domainList.Add(chkcompany.Text); if(chknetwork.Checked)domainList.Add(chknetwork.Text); } else { if(chkcom.Checked)domainList.Add(chkcom.Text); if(chknet.Checked)domainList.Add(chknet.Text); if(chkorg.Checked)domainList.Add(chkorg.Text); if(chkorgcn.Checked)domainList.Add(chkorgcn.Text); if(chkcn.Checked)domainList.Add(chkcn.Text); if(chkcomcn.Checked)domainList.Add(chkcomcn.Text); if(chknetcn.Checked)domainList.Add(chknetcn.Text); if(chkinfo.Checked)domainList.Add(chkinfo.Text); if(chkcouk.Checked)domainList.Add(chkcouk.Text); if(chkbiz.Checked)domainList.Add(chkbiz.Text); if(chkcc.Checked)domainList.Add(chkcc.Text); if(chktv.Checked)domainList.Add(chktv.Text); if(chkorguk.Checked)domainList.Add(chkorguk.Text); if(chkus.Checked)domainList.Add(chkus.Text); if(chkmeuk.Checked)domainList.Add(chkmeuk.Text); if(chkltduk.Checked)domainList.Add(chkltduk.Text); if(chkin.Checked)domainList.Add(chkin.Text); } Session["localpnlDomainName"]=domainList; //将首页查询的域名结果显示出来 if(Session["localpnlDomainName"]!=null) { IList<string>il=(IList<string>)Session["localpnlDomainName"]; if(il.Count>1) { stringdm=il[0]; stringdname; Labellbl; for(inti=1;i<il.Count;i++) { dname=dm+il[i]; if(IsReg(dname)) { lbl=newLabel(); lbl.ID="lbl"+i.ToString(); lbl.Text=string.Format("<pstyle='font-size:12px;color:green'>{0} 可以注册!</p>",dm+il[i]); indexpnlDomainName.Controls.Add(lbl); } else { lbl=newLabel(); lbl.ID="lbl"+i.ToString(); lbl.Text=string.Format("<p><atarget='_blank'style='color:red;font-size:12px'title='点击跳转到该域名'href='http://www.{0}'>{1} 已被注册!</a> <atarget='_blank'style='font-size:12px;color:red'title='点击查看详细信息'href='http://whois.hichina.com/cgi-bin/whois?domain={2}'>[查看]</a></p>",dm+il[i],dm+il[i],Server.UrlEncode(dm+il[i])); indexpnlDomainName.Controls.Add(lbl); } } } } } }
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net字符串操作技巧汇总》、《asp.net操作json技巧总结》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.netajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。