'undefined' appears in the message

I use Javascript to check some code and it works fine, but whenever I raise a warning to show errors, "undefined" appears at the beginning of the warning. Therefore, when I should expect a warning to show “Please enter a low target,” instead I get “undefined”, enter “Low target”. Can someone 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);
        }
+3
source share
2 answers

Assign a default value errorList, for example. empty line

var errorList = "";

While you do this, the initial value errorListis equal undefined.

+8
source

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

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

(, , .)

0

Source: https://habr.com/ru/post/1792544/


All Articles