Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping browser HTML form validation when submitting with javascript?

Is it possible to keep the default HTML validation if submitting via Javascript?

What I mean is that, if I submit a form using this JS method:

document.getElementById("mc-embedded-subscribe-form").submit();

How do I keep the defualt error messages thrown by the browser?

enter image description here

One workaround I thought of is using this:

<form onSubmit="return somefunction()">

But because the API returns the success inside a closure function, I can't use this method.

like image 975
James Lee Avatar asked Sep 01 '16 10:09

James Lee


1 Answers

according to my understanding of your question, html validation is not enough to halt submission, you have to validate required inputs in javascript too before submitting.

e.g

if (!empty(username)) {
    document.getElementById("mc-embedded-subscribe-form").submit();
}
like image 195
Bakhtawar GIll Avatar answered Nov 15 '22 00:11

Bakhtawar GIll