Javascript redirect URL

Javascript redirect URL

本文关键字:URL redirect Javascript      更新时间:2023-09-26

下面是我在与网站表单相关的脚本。如果前两个函数无效,我试图让它重定向到特定的页面。

发生的情况是,即使函数是有效的,重定向也会发生

我肯定我错过了一些非常简单的东西。。。

感谢您的帮助!

(function(){
var f1 = fieldname2,
    valid_pickup_postcode = function (postcode) {
    postcode = postcode.replace(/'s/g, "");
    var regex = /^[O,X]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
    return regex.test(postcode);
    };
var f2 = fieldname7,
    valid_dropoff_postcode = function (postcode) {
    postcode = postcode.replace(/'s/g, "");
    var regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
    return regex.test(postcode);
};
if( AND(f1,f2))
{
   if( valid_pickup_postcode(f1) && valid_dropoff_postcode(f2))
   {
      return 'Please select the vehicle you require for your delivery';
   }
   else
   {
      return window.location.href = "http://www.bing.com";
   }
}
else
{
return '';
}
})()
(function() {
    var f1 = fieldname2,
        valid_pickup_postcode = function(postcode) {
            postcode = postcode.replace(/'s/g, "");
            var regex = /^[O,X]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
            return regex.test(postcode);
        };
    var f2 = fieldname7,
        valid_dropoff_postcode = function(postcode) {
            postcode = postcode.replace(/'s/g, "");
            var regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
            return regex.test(postcode);
        };
    if (AND(f1, f2)) {
        if (valid_pickup_postcode(f1) && valid_dropoff_postcode(f2)) {
            return 'Please select the vehicle you require for your delivery';
        } else {
            // return window.location.href = "http://www.bing.com";
            window.location.replace("http://www.bing.com");
        }
    } else {
        return '';
    }
})()

window.location.replace("http://www.bing.com");应该完成

更新:我做了一些小的更改以使您的代码正常工作。对于像验证取件和送件邮政编码这样简单的东西,JS不是(或者不应该)非常复杂:)这里有一个更简单的版本,可以使用

function myValidator(f1, f2) {
    // Validate pickup postal code
    function pickup_postcode(postcode) {
        if (postcode) {
            if (isNaN(postcode)) {
                postcode = postcode.replace(/'s/g, "");
                var regex = /^[O,X]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
                return regex.test(postcode);
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    // Validate dropoff postal code
    function dropoff_postcode(postcode) {
        if (postcode) {
            if (isNaN(postcode)) {
                postcode = postcode.replace(/'s/g, "");
                var regex = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
                return regex.test(postcode);
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    if (pickup_postcode(f1) === true && dropoff_postcode(f2) === true) { // If both pickup and dropoff postal codes are ok return a message prompting vehicle selection
        return 'Please select the vehicle you require for your delivery';
    } else { // Invalid pickup or dropoff postal code
        // Redirect to website because either pickup or dropoff postal code is invalid
        window.location.replace("https://www.bing.com");
    }
}
myValidator("X909EF", "X909EE"); // Call it this way