Javascript表单验证。如何使用外部.js验证脚本

时间:2017-09-09 12:38:38

标签: javascript

我在使用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;
}

订单是我的html中的表单名称,Postcode是我要使用javascript验证的名称属性..

我做错了什么?

1 个答案:

答案 0 :(得分:0)

由于您已经拥有了邮政编码验证功能,因此您只需要为表单数据调用它。

if ( IsValid4DigitZip(document.orderbooks.Postcode.value) ) {
  // Zip code is valid
} else {
  // Zip code is invalid
}
相关问题