精確控制輸入框的中英文輸入字?jǐn)?shù)
<script language="javascript" type="text/javascript">
<!--
function limitLength(value, byteLength, attribute)
{
var newvalue = value.replace(/[^\x00-\xff]/g, "**");
var length = newvalue.length;
//當(dāng)填寫的字節(jié)數(shù)小于設(shè)置的字節(jié)數(shù)
if (length * 1 <=byteLength * 1)
{
return;
}
var limitDate = newvalue.substr(0, byteLength);
var count = 0;
var limitvalue = "";
for (var i = 0; i < limitDate.length; i++)
{
var flat = limitDate.substr(i, 1);
if (flat == "*")
{
count++;
}
}
var size = 0;
var istar = newvalue.substr(byteLength * 1 - 1, 1);//校驗點是否為“×”
//if 基點是×; 判斷在基點內(nèi)有×為偶數(shù)還是奇數(shù)
if (count % 2 == 0)
{
//當(dāng)為偶數(shù)時
size = count / 2 + (byteLength * 1 - count);
limitvalue = value.substr(0, size);
}
else
{
//當(dāng)為奇數(shù)時
size = (count - 1) / 2 + (byteLength * 1 - count);
limitvalue = value.substr(0, size);
}
alert("最大輸入" + byteLength + "個字節(jié)(相當(dāng)于"+byteLength /2+"個漢字)!");
document.getElementById(attribute).value = limitvalue;
//document.getElementById("msg").innerHTML= byteLength;
return;
}
//-->
</script>
<asp:TextBox runat="server" ID="txtComment" Height="77px"
ontextchanged="txtComment_TextChanged" TextMode="MultiLine" Width="307px"></asp:TextBox>
<textarea runat="server" cols="50" rows="10" name="explain" id="explain"></textarea>
在后臺page_load中加入:
explain.Attributes["onpropertychange"] = "limitLength(this.value,10,'explain')";
完整的頁面前臺代碼是:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<!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>DropdownList</title>
<script language="javascript" type="text/javascript">
<!--
function limitLength(value, byteLength, attribute)
{
var newvalue = value.replace(/[^\x00-\xff]/g, "**");
var length = newvalue.length;
//當(dāng)填寫的字節(jié)數(shù)小于設(shè)置的字節(jié)數(shù)
if (length * 1 <=byteLength * 1)
{
return;
}
var limitDate = newvalue.substr(0, byteLength);
var count = 0;
var limitvalue = "";
for (var i = 0; i < limitDate.length; i++)
{
var flat = limitDate.substr(i, 1);
if (flat == "*")
{
count++;
}
}
var size = 0;
var istar = newvalue.substr(byteLength * 1 - 1, 1);//校驗點是否為“×”
//if 基點是×; 判斷在基點內(nèi)有×為偶數(shù)還是奇數(shù)
if (count % 2 == 0)
{
//當(dāng)為偶數(shù)時
size = count / 2 + (byteLength * 1 - count);
limitvalue = value.substr(0, size);
}
else
{
//當(dāng)為奇數(shù)時
size = (count - 1) / 2 + (byteLength * 1 - count);
limitvalue = value.substr(0, size);
}
alert("最大輸入" + byteLength + "個英文(或"+byteLength /2+"個漢字)!");
document.getElementById(attribute).value = limitvalue;
//document.getElementById("msg").innerHTML= byteLength;
return;
}
//-->
</script>
</head>
<body>
<form id="form1" runat="server">
<textarea runat="server" cols="50" rows="10" name="explain" id="explain"></textarea>
</form>
</body>
</html>

浙公網(wǎng)安備 33010602011771號