Some suggestions or advice please. I'm trying to validate a multi-form page but I'm not sure how to specify the form in jquery selector:
<form id="form_a">
<label>First Name</name><input type="text" class="required"><br>
<label>Email</label><input type="text" class="required">
<button onclick="validate('form_a')">Submit</button>
</form>
<form id="form_b">
<label>Serial No </name><input type="text" class="required"><br>
<label>Brand </label><input type="text" class="required">
<button onclick="validate('form_b')">Submit</button>
</form>
<form id="form_c">
<label>First Name</name><input type="text" class="required"><br>
<label>Email</label><input type="text" class="required">
<button onclick="validate('form_c')">Submit</button>
</form>
<script>
function validate(whichform) {
$(whichform+" .required").each(function(i){
if ($(this).val().indexOf() < 0){
alert("null value detected")
$(this).css("border","1px solid red")
}
});
}
</script>
In your caase you are passing the id to the method, but you are not using the id selector, also you will have to return false from the event handler if you want to prevent the submission of the form
so
Demo: Fiddle
But a more jQuerish solution will be is to use jQuery event handlers like
then
Demo: Fiddle
Try this.
And remove the
onclick=""
from your html. As a best practice, try to avoid inline Javascript. Fiddle