我要做的是检查他们是否为区号代码输入3个数字,然后当他们输入3时移动到下一个字段。必须输入3个数字。我已经尝试过几种方式。
更新: 我还试图确保他们在区号,电话前缀和手机后缀中的4个号码中添加3个必需的3位数字。
我想我不应该使用onblur ......我想......
表格代码:
<div class="formPhone" style="float:left;">
(<input name="areaCode" id="areaCode" type="text" maxlength="3"
onkeyup="validateNextField(this,3,phonePrefix);"
onblur="validateFixedLengthNumericField(this,3);"
style="font-size:11px; width:20px;" title="Area Code" autocomplete="off">)
<input name="phonePrefix" id="phonePrefix" type="text"
onkeyup="validateNextField(this,3,phoneSuffix);"
onblur="validateFixedLengthNumericField(this,3);"
style="font-size:11px; width:20px;" maxlength="3" title="Phone Prefix" autocomplete="off">-
<input name="phoneSuffix" id="phoneSuffix" type="text" maxlength="4"
onkeyup="validateNextField(this,3,phoneExtension);"
onblur="validateFixedLengthNumericField(this,4);"
style="font-size:11px; width:25px;" title="Phone Suffix" autocomplete="off">
ext:
<input name="phoneExtension" id="phoneExtension" type="text" maxlength="5"
onkeyup="validateNextField(this,5,dsnPrefix);"
style="font-size:11px; width:25px;" title="Phone Extension" autocomplete="off">
</div>
JavaScript代码
function validateFixedLengthNumericField(numericField, len){
if (len != numericField.value.length){
numericField.focus();
alert('Field must contain exactly '+len+' numbers.');
}
}
// Once the max length is reached it forwards to the next field.
function validateNextField(numericField, len, nextField){
if (len == numericField.value.length){
nextField.focus();
}
}
答案 0 :(得分:0)
这应该适合你:
document.getElementById('areaCode').addEventListener('keyup',function(){
if(this.value.length == 3)
{
document.getElementById('phonePrefix').focus();
}
},false);
document.getElementById('phonePrefix').addEventListener('keyup',function(){
if(this.value.length == 3)
{
document.getElementById('phoneSuffix').focus();
}
},false);