本文实例讲述了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程序设计有所帮助。

点赞(0)

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部