I am making a form. I need to give feedback if form is invalid by changing button style and text so user know that their action they are trying to perform is not performed.
Here is what I got to run function if form is valid, which work perfectly.
$('#form').submit(function(event){
// cancels the form submission
event.preventDefault();
// do stuff if form valid
$("#btn-copy").html('COPY SUCCESS')
console.log('Text is copied.')
});
I need to run function if the form is invalid. If form is valid, both click and submit is registered so the two function collides. This is my current failed work around since if form valid button will say COPY FAIL, COPY SUCCESS, COPY, instead of COPY SUCCESS
$("#btn-copy").click(function(){
$(this).html('COPY FAIL');
setTimeout(function(){
$('#btn-copy').html('COPY');
},1000);
console.log('COPY FAIL.')
})
How do I run function only if submit button is clicked, but form is invalid (therefore, submit is not triggered). And the other way around if form is valid only run my valid function.
NOTE: Button has to be type:submit because of the first valid code I used, unless there is other options to achieve what I am trying to achieve since I don't want submit event anyway that's why I am doing event.preventDefault()