D
D
des1roer2015-10-08 08:08:23
JavaScript
des1roer, 2015-10-08 08:08:23

Reset error when removed from input?

When you enter non-numbers, I block the send button. but when the letters are deleted and the input is empty, the button is not unlocked

<script>           
            var tmp = "id[<?php echo $id; ?>]";
            var elem = document.getElementById(tmp);

            elem.onchange  = function () {
                this.value = (this.value.replace(',', "."));
                if (isNaN(this.value)) { // введено не число
                    // показать ошибку
                    this.className = "error";
                    error.innerHTML = 'Вы ввели не число. Исправьте, пожалуйста.'

                    document.getElementById("submit").disabled = true;
                }
                else
                if (this.className == 'error' || this.value == "") { // сбросить состояние "ошибка", если оно есть
                    this.className = "";
                    error.innerHTML = "";
                    document.getElementById("submit").disabled = false;
                }
            };
        </script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
des1roer, 2015-10-08
@des1roer

all chiki farts. in the wrong place rules the code))))

<script>
    var tmp = "id[<?php echo $id; ?>]";
    var elem = document.getElementById(tmp);

    elem.oninput = function () {
        this.value = (this.value.replace(',', "."));
        if (isNaN(this.value)) { // введено не число
            // показать ошибку
            this.className = "error";
            error.innerHTML = 'Вы ввели не число. Исправьте, пожалуйста.'
            document.getElementById("submit").disabled = true;
        }
        else
        if (this.className == 'error' || this.value == '') { // сбросить состояние "ошибка", если оно есть
            this.className = "";
            error.innerHTML = "";
            document.getElementById("submit").disabled = false;
        }
    };
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question