验证Ext JS javascript中的Null值

时间:2012-09-13 14:27:05

标签: javascript extjs

我需要允许值's1pdtCalc'为null并允许保存记录。 现在我收到错误消息“s1pdtCalc为null或不是对象”。感谢您的帮助,这是代码。

 function validateForm(values) {
        var pass = true;
        // check percent days turnaround

        var ck = values.s1pdtCalc.toString ();
        if (ck > "") {
            var t1 = values.s1pdtNTTd.toString (); //NEMIS Turn around
            var t2 = values.s1pdtTAd.toString (); //NEMIS Turn adjustment
            var t3 = values.actDays.toString ();  //NEMIS Turn adjustment
            t1 = (t1!=null?t1.trim ():0);

            if (ck == "MINUS") {
                if ((t1-t2) > t3) {
                    errorMsgs += '<br /> s1pdtATT - Percent days turnaround < 4.0.0. exceeds the number of activation days';
                }
            }
            else {
                if ((t1+t2) > t3) {
                    errorMsgs += '<br /> s1pdtATT - Percent days turnaround < 4.0.0. exceeds the number of activation days';
                }
            }
        }

        if (errorMsgs > "") {
            pass = false
        }
        return pass;
    }

1 个答案:

答案 0 :(得分:1)

您无法在不存在的对象上调用方法。如果在尝试调用方法之前它不存在,您需要将s1pdtCalc默认为某些内容:

function validateForm(values) {
    ...
    // set ck to an empty string if values.s1pdtCalc doesn't exist
    var ck = values.s1pdtCalc ? values.s1pdtCalc.toString() : '';
    ...