I'm trying to implement HTML validation in my project. Seems very helpful and easy for use. However this validation will work only if input type is set to submit
. That part is causing problem for me. I have multiple forms and I use same name
attribute on all Submit buttons. So my function looks like this:
HTML:
<form name="myForm" id="myForm" method="POST" action="#">
<fieldset>
<legend>User Info</legend>
<div class="formItem">
<label for="last_name">Last Name:</label>
<input type="text" name="frmst_lname" id="frmst_lname" value="" size="20" maxlength="30" title="Maximum length 30 characters." required/>
</div>
<div class="formItem">
<label for="first_name">First Name:</label>
<input type="text" name="frmst_fname" id="frmst_fname" value="" size="20" maxlength="30" title="Maximum length 30 characters." required/>
</div>
//Same button I have on the other forms. Only ID is different.
<div class="formItem">
<p align="center"><input type="button" name="frmSubmit" id="frmstSubmit" value="Submit"></p>
</div>
</fieldset>
</form>
jQuery:
$('input[name="frmSubmit"]').on('click', function(){
var frmID = $(this).closest('form').prop('id'); //take form ID
var processVal = $(this).attr('data-process'); //which process will be executed Add or Edit
//Here I would like to check form validation then process AJAX call
});
So far I couldn't get the HTML validation to work with onClick
function. I tried to use checkValidty()
but message never showed up for required fields on my form. I'm wondering if HTML is good fit for my project and how I can get validation to work with onClick function?