I'm new to JavaScript. I need to prevent the form from submitting if any of the validation conditions returned false.
ex:-
if((document.getElementById("name").value)==""){
alert("'Name' field can not be empty");
return false;
}
I tried <form onsubmit="return validate();">
and <form onsubmit="event.preventDefault(); validate();">
. None of those helped. First one prompt the same alert message twice. Second one continuously prompt all alert messages. What I want is,
- If there is one false condition, I need the browser to stop the form from submitting and prompt the relevant alert message.
- If there are more than one false conditions, I need the browser to stop the form from submitting and only prompt one alert message.
Is there any way to accomplish this by using JavaScript (No JavaScript libraries)?
Sorry if there are any grammar mistakes, English is not my first language :(