admin管理员组

文章数量:1277900

I am using Javascript to validate some code, and it works fine, but whenever I call alert to show the errors, at the beginning of the alert message I get 'undefined'. So when I should expect the alert to show 'Please enter a Low Target', instead I get 'undefinedPlease enter a Low Target'. Can somebody tell me what is wrong with my code?

//validation
        var lowTarget;
        var highTarget;
        var errorList;
        var isValid = true;

        lowTarget = $('input[name="txtLowTarget"]').val();
        highTarget = $('input[name="txtHighTarget"]').val();

        if (lowTarget == "") {
            errorList += "Please enter a Low Target\n";
            isValid = false;
        }
        else {
            if (isNumeric(lowTarget) == false) {
                errorList += "Low Target must be numeric\n";
                isValid = false;
            }
        }

        if (highTarget == "") {
            errorList += "Please enter a High Target\n";
            isValid = false;
        }
        else {
            if (isNumeric(highTarget) == false) {
                errorList += "High Target must be numeric\n";
                isValid = false;
            }
        }

        if (isValid == true) {
            if (!(parseFloat(highTarget) > parseFloat(lowTarget))) {
                errorList += "High Target must be higher than Low Target\n";
                isValid = false;
            }
        }

        if (isValid == false) {
            alert(errorList);
        }

I am using Javascript to validate some code, and it works fine, but whenever I call alert to show the errors, at the beginning of the alert message I get 'undefined'. So when I should expect the alert to show 'Please enter a Low Target', instead I get 'undefinedPlease enter a Low Target'. Can somebody tell me what is wrong with my code?

//validation
        var lowTarget;
        var highTarget;
        var errorList;
        var isValid = true;

        lowTarget = $('input[name="txtLowTarget"]').val();
        highTarget = $('input[name="txtHighTarget"]').val();

        if (lowTarget == "") {
            errorList += "Please enter a Low Target\n";
            isValid = false;
        }
        else {
            if (isNumeric(lowTarget) == false) {
                errorList += "Low Target must be numeric\n";
                isValid = false;
            }
        }

        if (highTarget == "") {
            errorList += "Please enter a High Target\n";
            isValid = false;
        }
        else {
            if (isNumeric(highTarget) == false) {
                errorList += "High Target must be numeric\n";
                isValid = false;
            }
        }

        if (isValid == true) {
            if (!(parseFloat(highTarget) > parseFloat(lowTarget))) {
                errorList += "High Target must be higher than Low Target\n";
                isValid = false;
            }
        }

        if (isValid == false) {
            alert(errorList);
        }
Share Improve this question asked Feb 16, 2011 at 14:27 user517406user517406 13.8k30 gold badges84 silver badges122 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Assign some default value to errorList, e.g. empty string

var errorList = "";

Until you do that, initial value of errorList is undefined.

I was finding the same problem in my project while using

var try2 = document.getElementsByName("y_email").value;
alert(try2);

Now I used the following and it works well

 var try2 = document.getElementsByName("y_email")[0].value;
 alert(try2);

(So Be doubly sure that what you are using in correct format to use.)

本文标签: javascript39undefined39 appearing in alertStack Overflow