![](/img/trans.png)
[英]Javascript form validation. Radio buttons and checkboxes. How to validate?
[英]Javascript form validation. How to use external .js validation script
我在Javascript表单验证中遇到麻烦。 我不知道如何将外部.js脚本与验证规则一起使用,并将其插入html文件中,并使它们正常工作。
我已将脚本链接到开头部分,如下所示:
这就是.js验证脚本中的内容
function IsValid4DigitZip( str )
if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
return false;
var isValid = true;
str += "";
if (IsBlank(str) || (str.length != 4) || !IsInt(str, false))
isValid = false;
return isValid;
}
我尝试将其插入以下部分
function IsValid4DigitZip(document.orderbooks.Postcode) {
if (document.orderbooks.Postcode.value+"" == "undefined" || document.orderbooks.Postcode.value+"" == "null" || document.orderbooks.Postcode.value+"" == "")
alert("Invalid Postcode !")
return false;
var isValid = true;
document.orderbooks.Postcode += "";
if (IsBlank(document.orderbooks.Postcode) || (document.orderbooks.Postcodestr.length != 4) || !IsInt(document.orderbooks.Postcode, false))
alert("Invalid Postcode!")
isValid = false;
return isValid;
}
orderbooks是我html中的表单名称,而Postcode是我要使用javascript验证的名称属性。
我究竟做错了什么?
由于您已经具有邮政编码验证功能,因此只需为表单数据调用它即可。
if ( IsValid4DigitZip(document.orderbooks.Postcode.value) ) {
// Zip code is valid
} else {
// Zip code is invalid
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.