我是JavaScript新手,我一直在做一些创建HTML和JavaScript表单的工作。在这项工作中,我一直试图根据输入到前一个字段中的文本来验证输入的格式。输入改变文本框格式
我一直在尝试的是,如果国家'澳大利亚'进入'国家'文本框比'电话'文本框被限制为格式(00)00000000,并且如果它是任何其他国家'电话'文本框必须采用国际号码格式,包括例如+和国家代码+61等
我已经做了这么多,到目前为止函数:
<script>
document.getElementById('txttelephone').onchange = function()
{
var num1 = document.getElementById('txttelephone').value,
country = document.getElementById('txtcountry').value,
regex;
if (country == "Australia" || country == "australia")
{
regex = /\(\d{2}\)\d{8}/;
}
else
{
regex = /\+\d{15}/;
}
if (!num1.match(regex))
{
alert('That is not a correct telephone number');
}
}
</script>
这仅仅是我做的“电话”文本框中的字符串长度限定为12个字符,但我仍然功能尚未验证以确保(00)00000000格式的区号包含在括号内,并且如果国家不是澳大利亚(包括国家代码在内的国际号码),则还要验证是否包含a +。
下面是HTML我不得不使用功能上:
<b>Country:</b> <input type="text" id="txtcountry" name="country">
<br>
<b>Telephone:</b> <input type="text" id="txttelephone" name="telephone">
任何帮助将不胜感激!
检查出[regex](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – 2013-04-05 14:08:42