87,989
社区成员
发帖
与我相关
我的任务
分享<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function chkTextareaLen(textareaId,maxLen) //textareaId指input框的id,maxLen指限制长度
{
var textareaValue = document.getElementById(textareaId).value;
var curLen = 0,substrLen = 0;
for (var i=0; i<textareaValue.length; i++)
{
if (textareaValue.charCodeAt(i)>127 || textareaValue.charCodeAt(i)==94)
{
curLen += 2;
}
else
{
curLen ++;
}
if(curLen > maxLen)
{
substrLen = i;
break;
}
}
if(curLen > maxLen)
{
if(substrLen == 0) substrLen = maxLen;
document.getElementById(textareaId).value = textareaValue.substring(0,substrLen);
alert("文本长度不能大于"+maxLen); //注:8个字节,如果是中文就只能输入四个字。
}
else
{
document.getElementById(textareaId).innerHTML = maxLen - curLen;
}
}
</script>
</head>
<body>
<div>
<input type="text" id="newName" name="newName" onkeyup="chkTextareaLen(this.id,8)">
</div>
<div>
<input type="text" id="newPass" name="newPass" onkeyup="chkTextareaLen(this.id,8)">
</div>
</body>
</html><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Insert title here</title>
<script type="text/javascript">
window.onload = function() {
var submitForm = document.getElementById("submitForm");
var newName = document.getElementById("newName");
submitForm.onsubmit = function() {
if (newName.value.length > 8) {
alert("xxxx");
return false;
}
return true;
};
};
</script>
</head>
<body>
<form id="submitForm" action="http://www.baidu.com" >
<div>
UserName:<input type="text" id="newName" name="newName">
</div>
<div>
Password:<input type="text" id="newPass" name="newPass">
</div>
<input type="submit">
</form>
</body>
</html>