<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ucControl._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> //方法一:前台 <mce:script type="text/javascript" language="javascript"><!-- function isChinese(str) { var lst = /[u00-uFF]/; return !lst.test(str); } function strlen(str) { var strlength = 0; for (i = 0; i < str.length; i++) { if (isChinese(str.charAt(i)) == true) strlength = strlength + 2; else strlength = strlength + 1; } return strlength; } function textLimitCheck(thisArea, maxLength) { var txt_name = document.getElementById(thisArea); if (strlen(txt_name.value) > maxLength) { alert(maxLength + "个字符限制!"); txt_name.focus(); } else { alert("ok"); } } // --></mce:script> </head> <body> <form id="form1" runat="server"> <div> 姓名:<input type="text" id="txt_name" /><input type="button" id="btn_check" οnclick="textLimitCheck('txt_name',10);" /> <br /> <br /> --------后台的做法----------------- <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:TextBox ID="tb_name" runat="server" /> <asp:Button ID="btn_checkVilidate" runat="server" OnClick="btn_checkVilidate_Click" /> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html> 方法二:后台 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ucControl { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public string stringFormat(string str, int n) { string temp = string.Empty; int t = 0; char[] q = str.ToCharArray(); if (System.Text.Encoding.Default.GetByteCount(str) <= n) //如果长度比需要的长度n小,返回原字符串 { for (int i = 0; i < q.Length && t < n; i++) { if ((int)q[i] >= 0x4E00 && (int)q[i] <= 0x9FA5) //是否汉字 { temp += q[i]; t += 2; } else { temp += q[i]; t += 1; } } if (t <= n) { return temp; } else return null; } else { return null; } } protected void btn_checkVilidate_Click(object sender, EventArgs e) { if (tb_name.Text.Length > 0) { string str = stringFormat(tb_name.Text, 10); if (str == null) ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('输入字符有问题!');", true); else tb_name.Text = str; ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('ok!');", true); } } } } 方法三: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ucControl.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <mce:script language="javascript" type="text/javascript"><!-- //判断输入的中英文 function betylength(str) { var byteLen = 0, len = str.length; if (str) { for (var i = 0; i < len; i++) { if (str.charCodeAt(i) > 255) {//一字节有8位2进制数,从0开始,2的8次方就是255;如果大于255,就表示占用了2个字节 byteLen += 2; } else { byteLen++; } } return byteLen; } else { return 0; } } //判断是否超出对应的字符长度10 function check() { var str = document.getElementById("txt_name").value; if (betylength(str) > 10) { alert("超过字符长度,请重复输入"); } return false; } // --></mce:script> </head> <body> <form id="form1" runat="server"> <div> <input type="text" id="txt_name" /> <input type="button" id="btn_check" οnclick="check();" /> </div> </form> </body> </html>