1 很久以前
<html><
head>
function setLength()
{
if (document.getElementById("radA").checked==true)
{document.form1.txtA.attributes['maxLength'].value=8;}
if (document.getElementById("radB").checked==true)
{document.form1.txtA.attributes['maxLength'].value=12;}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:RadioButton GroupName="a" ID="radA" runat=server Text="A"/><asp:RadioButton GroupName="a" ID ="radB" Text="B" runat=server /><br>
<asp:TextBox ID ="txtA" runat=server onfocus="setLength()"></asp:TextBox>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
2、使用js使输入文本自动转换大小写:
function ConvertStr(control, IsToUpper) {
var cValue = control.value;
if (IsToUpper == "1") {
control.value = cValue.toUpperCase();
}
else {
control.value = cValue.toLowerCase();
}
}
<txt:CustomTextBox ID="txtPrinterSN" runat="server" IsNull="false" TextType="Text" onkeyup="ConvertStr(this,1);"></txt:CustomTextBox>
3、使用js控制输入字符串长度
function checkLength(obj, maxlength, isChs) {
var conValue = obj.value;
if (conValue.length > maxlength) {
conValue = conValue.substring(0, maxlength);
obj.value = conValue;
}
if (isChs == "0") {
if (conValue.length > 0) {
if (/[^\x00-\xff]/g.test(conValue)) {
conValue = conValue.replace(/[\u4e00-\u9fa5]/g, '');
if (conValue.length > 0) {
obj.value = conValue;
}
else {
obj.value = "";
}
}
}
}
}
<asp:TextBox ID="txtFaultDescription_NotNull" runat="server" Width="500px" MaxLength="250" TextMode="MultiLine" onpropertychange="checkLength(this,290,0)"></asp:TextBox>