简体   繁体   English

Javascript 警报消息不起作用,为什么?

[英]Javascript alert message is not working, why?

Javascript alert message is not working in dev server but works in test instance? Javascript警报消息在开发服务器中不起作用但在测试实例中起作用? Please help to find the issue.请帮助查找问题。

if (expdt.value == "") {
                alert("Expiration Date must be entered");
                expdt.focus();
        formSubmitted = false;
                return false;
        }else
        {
                var dtpattern = /(0|1)[0-9]\/(19|20)[0-9]{2}/;
                if (dtpattern.test(expdt.value))
                {
                        var date_array = expdt.value.split('/');
                        var month = date_array[0]-1;
                        var year = date_array[1];

                        source_date = new Date();

                        if (year >= source_date.getFullYear())
                        {
                                if (month >= source_date.getMonth())
                                {
                                        return '';
                                } else {
                                        if (year == source_date.getFullYear())
                                        {
                                        alert("Expiration Date: Month must be greater than or equal to current month");
                                        expdt.focus();
                                        formSubmitted = false;
                                        return false;
                                        }
                                }
                        } else {
                                alert("Expiration Date: Year must be greater than or equal to current year");
                                expdt.focus();
                formSubmitted = false;
                                return false;
                        }

                }
                else
                {
                        alert("Expiration Date must match the format MM/YYYY");
                        expdt.focus();
                        formSubmitted = false;
                        return false;
                }
        }

 
        if (cardnumber.value == "") {
                alert("Card Number must be entered");
                cardnumber.focus();
                formSubmitted = false;
                return false;
        }

Only expiry date validation alert message is working in dev server but other alert messages after exp date validation also works in test instance.只有过期日期验证警报消息在开发服务器中有效,但过期日期验证后的其他警报消息也适用于测试实例。 What is the issue?问题是什么? Thanks in advance.提前致谢。

The reason your code doesn't execute the cardnumber validation is when您的代码不执行卡号验证的原因是

if (year >= source_date.getFullYear()) {
    if (month >= source_date.getMonth()) {
        return "";
    }

No more checking occurs when this condition is met because you've returned "" , that's it.满足此条件时不再进行检查,因为您已返回"" ,仅此而已。 Function over man!功能胜过人!

Try coding without using else - it makes code flatter and more readable在不使用else的情况下尝试编码 - 它使代码更扁平、更易读

Inverting some of the conditions (eg instead of if (dtpattern.test(expdt.value)) do if (!dtpattern.test(expdt.value)) since you return in that case) you can use a series of so-called "guard clauses" to drastically improve the readability of your code反转一些条件(例如,如果if (!dtpattern.test(expdt.value))因为你在这种情况下返回,而不是if (dtpattern.test(expdt.value)) ,你可以使用一系列所谓的“保护条款”,以极大地提高代码的可读性

An example guide on guard clauses - there's many more if you search.有关保护条款的示例指南- 如果您搜索,还有更多。

For example - your code is simply this例如-您的代码就是这样

if (expdt.value == "") {
    alert("Expiration Date must be entered");
    expdt.focus();
    formSubmitted = false;
    return false;
}

var dtpattern = /(0|1)[0-9]\/(19|20)[0-9]{2}/;
if (!dtpattern.test(expdt.value)) {
    alert("Expiration Date must match the format MM/YYYY");
    expdt.focus();
    formSubmitted = false;
    return false;
}

var date_array = expdt.value.split("/");
var month = date_array[0] - 1;
var year = date_array[1];

const source_date = new Date();
const source_year = source_date.getFullYear();
const source_month= source_date.getMonth();

if (year < source_year) {
    alert(
        "Expiration Date: Year must be greater than or equal to current year"
    );
    expdt.focus();
    formSubmitted = false;
    return false;
}

if (month < source_month && year == source_year) {
    alert(
        "Expiration Date: Month must be greater than or equal to current month"
    );
    expdt.focus();
    formSubmitted = false;
    return false;
}

if (cardnumber.value == "") {
    alert("Card Number must be entered");
    cardnumber.focus();
    formSubmitted = false;
    return false;
}

return "";

Notice how all the fail conditions are at the "root" indentation of the code?请注意所有失败条件如何位于代码的“根”缩进处?

Side note: I decided to remove else in a medium sized company PWA - I'm down to ONE in the whole project, and I could remove it fairly easily, but I find that remaining else to be more readable than the alternative旁注:我决定在一家中型公司 PWA 中删除else - 我在整个项目中只剩下一个,我可以很容易地删除它,但我发现剩下的else比替代方案更具可读性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM