js zip验证有效时执行js函数
我正在使用我在网上找到的这个邮政编码验证脚本,但我希望它在 zip 是有效格式时执行函数 ShipCalc() ,而不是仅仅提交表单并将其附加到网址末尾。我应该在哪里更改它来调用该函数?
JS
<script language="javascript">
function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;
if (field.length!=5 && field.length!=10) {
alert("Please enter your 5 digit or 5 digit+4 zip code.");
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code. Please try again.");
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");
return false;
}
}
return true;
}
</script>
HTML函数(如果有效)
<form name="form9" onSubmit="return validateZIP(this.zip.value)">
Zip: <input type="text" size="10" name="zip">
<input type="submit" value="Submit">
</form>
我需要调用的
<script language="javascript">
function shipCalc() {
$('#cartdialog').load("/ash/shop/shipping.php?zip=" + document.form9.zip.value);
}
</script>
I am using this zip code verification script I found online but instead of just submitting the form and appending it to the end of the url, I would like for it to execute function shipCalc() when the zip is a valid format. Where would I alter this to call the function?
JS
<script language="javascript">
function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;
if (field.length!=5 && field.length!=10) {
alert("Please enter your 5 digit or 5 digit+4 zip code.");
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code. Please try again.");
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");
return false;
}
}
return true;
}
</script>
HTML
<form name="form9" onSubmit="return validateZIP(this.zip.value)">
Zip: <input type="text" size="10" name="zip">
<input type="submit" value="Submit">
</form>
FUNCTION I NEED TO CALL IF VALID
<script language="javascript">
function shipCalc() {
$('#cartdialog').load("/ash/shop/shipping.php?zip=" + document.form9.zip.value);
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
return true;
行替换为:Replace the
return true;
line with: