如何从错误对象中显示验证表单中的错误

时间:2018-05-14 23:25:26

标签: javascript jquery validation

如何从对象中选择相应的类型来附加HTML中的文本?  我尝试从errorText中选择一个错误并将其推入errors数组中。并且不知道如何附加到HTML中。

        var btn = document.querySelector('.btn');
btn.addEventListener('click', checkForm);
var select = document.querySelector('.required');
var inputs = document.querySelectorAll('.form-element');
var errors = [];
var errorText = {
     1 : "The field 'First name' is empty",
     2:  "The field 'Last name' is empty",
     3:  "Pick your date of birth",
     4:  "Pick a country",
     5:  "The field 'E-mail' is empty",
     6:  "Enter your password",
     7:  "The field 'Address' is empty",
     8:  "Enter correct email",
     9:  "Current date is not allowed"
}

function checkForm() {
    for(i = 0; i < inputs.length; i++) {  
    if (inputs[i].value == "") {
        inputs[i].style.borderColor = 'red';
    }
}
   if(select.selectedIndex=="") {
    errors.push({el :4});
    select.appendChild(errors);
    // select.innerHTML = 'Please, select a country';
    return false;
   } else {
    return true;
   }
}

0 个答案:

没有答案