本文實例講述了asp.net實現中英文多域名檢測的方法。分享給大家供大家參考,具體如下:
第一步:在前台頁面中寫入js代碼和相關控件
/****寫入js代碼****/ <%-- 域名檢測 --%> <script type="text/javascript"> //判斷輸入的是否為中文域名 function IsChDomain(domainStr) { var flag = false; var compStr = "ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_0123456789"; var domainLength = domainStr.length;//判斷字符長度 for(var i = 0;i < domainLength;i++) { var temp = domainStr.charCodeAt(i); if(temp >= 10000)//含有字符編碼大於10000的字符判斷為中文,不太嚴格 { flag=true; } else { var temp2 = compStr.indexOf(domainStr.charAt(i)); if(temp2 == -1) { flag = false; break; } } } return flag; } //判斷輸入的是否為英文域名 function IsEnDomain(domainStr) { var flag = false; var compStr = "ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_0123456789"; var domainLength = domainStr.length; for(var i = 0;i<domainLength;i++) { if(compStr.indexOf(domainStr.charAt(i)) == -1) { flag = false; break; } else { flag = true; } } return flag; } //中國等不能注冊英文域名,把相應的CheckBox的disabled屬性設置為true, function Trim(domainStr) { return domainStr.replace(/(^s*)|(s*$)/g, ""); } //驗證域名是哪一類型的 function checkDomainType() { var domainName = document.getElementById("txtDomainName").value; domainName = Trim(domainName);//去掉輸入的特殊符號 if(IsChDomain(domainName))//調用中文域名----------驗證方法 { setCheckBox(true); } else if(IsEnDomain(domainName))//調用英文域名-----驗證方法 { setCheckBox(false); } } //為CheckBox復選框的Checked屬性賦值 function setCheckBox(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; } //檢查輸入的字符規范 // function checkValue() // { // if(document.getElementById("txtDomainName").value=='') // { // alert('請輸入域名!'); // return false; // } // if(document.getElementById("txtDomainName").value.length >= 60) // { // alert('域名長度不能超過60個字符!'); // return false; // } // for(var i = 0;i < document.getElementById("txtDomainName").value.length;i++) // { // if(document.getElementById("txtDomainName").value.charAt(i) == ' ') // { // alert('域名中不能含有空格!'); // return false; // break; // } // } // } </script> /***寫入相關控件***/ //用於顯示查詢的結果 <asp:Panel ID="indexpnlDomainName" runat="server" Width="100%" /> // <table width="373" border="0" cellpadding="0" cellspacing="1"> <tr> <td width="74" height="41"> <asp:CheckBox ID="chkcom" Text=".com" runat="server" /> </td> <td width="71" height="41"> <asp:CheckBox ID="chkcn" Text=".cn" runat="server" /> </td> <td width="79" height="41"> <asp:CheckBox ID="chkorg" Text=".org" runat="server" /> </td> <td width="71" height="41"> <asp:CheckBox ID="chknet" Text=".net" runat="server" /> </td> <td width="72" height="41"> <asp:CheckBox ID="chkinfo" Text=".info" runat="server" /> </td> </tr> <tr> <td width="74" height="41"> <asp:CheckBox ID="chkcomcn" Text=".com.cn" runat="server" /> </td> <td height="41"> <asp:CheckBox ID="chkcc" Text=".cc" runat="server" /> </td> <td height="41"> <asp:CheckBox ID="chkorgcn" Text=".org.cn" runat="server" /> </td> <td height="41"> <asp:CheckBox ID="chknetcn" Text=".net.cn" runat="server" /> </td> <td height="41"> <asp:CheckBox ID="chkin" Text=".in" runat="server" /> </td> </tr> <tr> <td width="74" height="40"> <asp:CheckBox ID="chkcouk" Text=".co.uk" runat="server" /> </td> <td height="41"> <asp:CheckBox ID="chktv" Text=".tv" runat="server" /> </td> <td height="41"> <asp:CheckBox ID="chkorguk" Text=".org.uk" runat="server" /> </td> <td height="41"> <asp:CheckBox ID="chkus" Text=".us" runat="server" /> </td> <td height="41"> <asp:CheckBox ID="chkmeuk" Text=".me.uk" runat="server" /> </td> </tr> <tr> <td width="74" height="41"> <asp:CheckBox ID="chkltduk" Text=".ltd.uk" runat="server" /> </td> <td height="41"> <asp:CheckBox ID="chkbiz" Text=".biz" runat="server" /> </td> <td height="41"> <asp:CheckBox ID="chknetwork" Text=".網絡" runat="server" /> </td> <td height="41"> <asp:CheckBox ID="chkcompany" Text=".公司" runat="server" /> </td> <td height="41"> <asp:CheckBox ID="chkchina" Text=".中國" runat="server" /> </td> </tr> </table>
第二步:在後台頁面中寫入方法
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Text; //(需要引入的類文件命名空間) using System.Net.Sockets; //(需要引入的類文件命名空間) using System.Text.RegularExpressions; //(需要引入的類文件命名空間) using System.Collections.Generic; //(需要引入的類文件命名空間) using System.Data.Common; //(需要引入的類文件命名空間) using System.Xml; //(需要引入的類文件命名空間) using System.IO; //(需要引入的類文件命名空間) using Microsoft.SqlServer.Server; //(需要引入的類文件命名空間) using System.Net; //(需要引入的類文件命名空間) //判斷是否為中文域名(方法) public static bool IsHasCHZN(string domainName) { Regex RegCHZN = new Regex("[一-龥]"); Match m = RegCHZN.Match(domainName); return m.Success; } //判斷域名是否被注冊(方法) public static bool IsReg(string domainName) { bool flag = false; string dm = HttpContext.Current.Server.UrlEncode(domainName); try { //判斷方法非常多,如打開遠程文件再處理字符串等等,這裡用的方法效率不是很高 WebClient wc = new WebClient(); string xmlstr = wc.DownloadString("http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=" + dm); StringReader sr = new StringReader(xmlstr); XmlTextReader xr = new XmlTextReader(sr); while (xr.Read()) { if (xr.IsStartElement("original")) { xr.Read(); if (xr.Value.Substring(0, 3) == "210") { flag = true; break; } else { flag = false; break; } } } return flag; } catch { return false; } } //按鈕事件中 protected void imgBtnCheck_Click(object sender, ImageClickEventArgs e) { string txtYM = this.txtYMname.Text.Trim(); if (txtYM == "") { ScriptManager.RegisterStartupScript(this, this.GetType(), "sc", "alert('對不起,域名不能為空!');", true); } else { string domainName = this.txtYMname.Text.Trim(); IList<string> domainList = new List<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) { string dm = il[0]; string dname; Label lbl; for (int i = 1; i < il.Count; i++) { dname = dm + il[i]; if (IsReg(dname)) { lbl = new Label(); lbl.ID = "lbl" + i.ToString(); lbl.Text = string.Format("<p style='font-size:12px;color:green'>{0} 可以注冊!</p>", dm + il[i]); indexpnlDomainName.Controls.Add(lbl); } else { lbl = new Label(); lbl.ID = "lbl" + i.ToString(); lbl.Text = string.Format("<p><a target='_blank' style='color:red;font-size:12px' title='點擊跳轉到該域名' href='http://www.{0}'>{1} 已被注冊!</a> <a target='_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.net ajax技巧總結專題》及《asp.net緩存操作技巧總結》。
希望本文所述對大家asp.net程序設計有所幫助。