Answer the question
In order to leave comments, you need to log in
How to make ajax validate a field before submitting a form?
I want to make a validateField function that will send the field value to the server by Ajax and receive a true or false response
. How can I attach this function to the form submit event?
I would like to make these functionals independent of each other.
Answer the question
In order to leave comments, you need to log in
<input name="test1" class="js-validate" data-validate-name="validate1" />
<input name="test2" class="js-validate" data-validate-name="validate2" />
document.querySelectorAll('.js-validate').forEach(function(item) {
item.addEventListener('onchange', function(event){
// event.target - измененный элемент
// event.target.value
// event.target.getAttribete('data-validate-name') - какую валидацию вы хотите
// тут вызывать Ajax запрос с требуемыми параметрами, когда придёт ответ
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question