常見的有:手機号,電話,驗證碼,序列号
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXauYjM5cmbvlXZkVHavw1LcpDc0RHaiojIsJye.png)
<html>
<head>
<script type="text/javascript">
function checklen(x,y)
{
if (y.length==x.maxlength)
{
var next=x.tabindex
if (next<document.getelementbyid("myform").length)
{
document.getelementbyid("myform").elements[next].focus()
}
}
}
</script>
</head>
<body>
<p>這段腳本在達到文本框的最大長度時跳到下一個文本框:</p>
<form id="myform">
<input size="3" tabindex="1" maxlength="3" onkeyup="checklen(this,this.value)">
<input size="2" tabindex="2" maxlength="2" onkeyup="checklen(this,this.value)">
<input size="3" tabindex="3" maxlength="3" onkeyup="checklen(this,this.value)">
</form>
</body>
</html>
開發過程中用寫的一個腳本,記錄下來以備後用與他用,其中attributes["max"].nodevalue是取html自定義的 max屬性(相容firefox和ie)
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXauYjM5cmbvlXZkVHavw1LcpDc0RHaiojIsJye.png)
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>輸入cd-key自動分割</title>
<script language="javascript" type="text/javascript">
function cdkeyinput(thisobj)
objresult = "";
nextobjresult = "";
if(thisobj.value.indexof('-') > 0)
{
objresult = thisobj.value.substring(0,thisobj.value.indexof('-'));
nextobjresult = thisobj.value.substring(thisobj.value.indexof('-')+1);
thisobj.value = objresult.substring(0,thisobj.attributes["max"].nodevalue);
if(arguments[1] != undefined && arguments[1] != null)
{
next = arguments[1];
nextobj = document.getelementbyid(next);
nextobj.value = nextobjresult.touppercase();
nextobj.onkeyup();
}
}
thisobj.value = thisobj.value.touppercase();
</br>
<input type="text" name="key1" id="key1" max="8" size="8" onkeyup="cdkeyinput(this,'key2');"/>-
<input type="text" name="key2" id="key2" max="4" size="4" onkeyup="cdkeyinput(this,'key3');"/>-
<input type="text" name="key3" id="key3" max="4" size="4" onkeyup="cdkeyinput(this,'key4');"/>-
<input type="text" name="key4" id="key4" max="4" size="4" onkeyup="cdkeyinput(this,'key5');"/>-
<input type="text" name="key5" id="key5" max="4" size="4" onkeyup="cdkeyinput(this,'key6');"/>-
<input type="text" name="key6" id="key6" max="8" size="8" onkeyup="cdkeyinput(this);"/>