1

我在登记表上有一个收集电话区号的文本框——我聪明的自己认为我的客户不会那么好输入 5 位数的邮政编码而不是 3 位数的区号,所以现在我需要制作一个 jquery或用于验证条目的javascript函数是###,###,###,###,###...格式 他们可以输入的区号数量没有限制。问题是我不知道代码应该如何运行 - 我的文本框有任何想法

<input type="text" id="acode" name="acode" />

我很感激

完成的代码

HTML

  <label for="acode">Enter area codes separated by commas (757,804,252,###):</label>
  <input type="text" id="acode" name="acode" onblur="valarea(this.form)"/>

Javascript

<script>
function valarea(form)
{
inputvalue =form.acode.value;
var regexp = /^\d{3}(?:,\d{3})*$/;
if (inputvalue != "")
{
if (regexp.test(inputvalue)) {
//nothing happens
} else {
alert('You have entered an invalid value, you should have listed 3 digit telephone area codes for the areas you are interested in shopping separated by commas (757,804,252,###) Please Correct the problem before continuing')
form.acode.focus()
}
}
}
4

2 回答 2

1

使用正则表达式:

var regexp = /^\d{3}(?:,\d{3})*$/;
if (regexp.test(inputvalue)) {
  // Valid, do something with it...
} else {
  // Invalid, show error.
}

有关 Javascript 中正则表达式的更多信息,请参阅 MDN 正则表达式页面

于 2012-10-15T20:55:18.183 回答
0

不确定您是否需要它,但如果您想验证当前区号的列表,我可以给您发送一个列表。我在GreatData.com工作,我们维护美国和加拿大的当前区号。只需访问该网站获取联系信息并索取文件即可。

不知道您的确切应用程序,但您可能有一个选择列表,他们可以在其中选择适当的区号并自己构建逗号分隔列表。

于 2012-10-31T01:08:10.313 回答