-1
You can add a Listener in the input
who is trying to perform this validation, but would like to make it clear that client validations are only part of the process, it is always good to validate the data in the backend.
But let’s go to the code:
$('#id-do-input').on('change', function() {
var value = $(this).val()
if (!value) {
$(this).removeClass('text-success')
$(this).addClass('text-error')
} else {
$(this).removeClass('text-error')
$(this).addClass('text-success')
}
})
And if you want to do this validation by pressing the send button, just add the same Listener above, but changing all occurrences of $(this)
for $('#id-do-input')
.
You will need to validate the input when submitting the form using JS, then style your HTML using CSS if the field value is invalid. It is a very comprehensive question, where exactly is your doubt? What have you tried so far?
– fernandosavio
Put in the code you already have.
– Sam