I have a form but all fields need to be filled in before the form will submit. I have experimented with adding HTML5 required = "required" which works fine on Chrome but not on IE.
Form field names are dynamic so I cannot reference them directly so needing some JavaScript code to loop through the inputs in the form then if the value is blank to focus the field with an alert and return false on the form submit.
I have started putting something together but my javascript is terrible ! Hoping yours is much better !
<script type="text/javascript">
function validatelink2(){
for (var i = 0; i < document.forms['form1bxxx'].elements.length; i++) {
var e = document.forms['form1bxxx'].elements[i];
alert(e);
}
</script>
<form id="form1bxxx" name="form1bxxx" method="post" action="submitform.asp" onSubmit="return validateForm2(this)">
<input name="veh<%=id%>" value="" type="text" id="veh<%=id%>" size="7" required="required" >
<input name="date<%=id%>" type="text" id="date<%=id%>" size="10" onFocus="showCalendar('',this,this,'','holder1',0,30,1)">
<input name="mile<%=id%>" type="text" id="mile<%=id%>" size="6">
<input type="submit" name="button999" id="button999" value="Continue >>>" onclick="return validatelink2(this);" />
Edit....
Updated Javascipt to
<script type="text/javascript">
function validatelink2(){
//Make sure required items are filled in
alert("hello");
var x=document.getElementsByClassName('required');
for(var i = 0; i <x.length; i++){
alert("checking");
if (x[i].value == null || x[i].value == "")
{
x[i].focus();
alert("All Required Items are not completed.");
return false;
}
}
}
</script>
and I've set class required to have a background colour of yellow so I can see all the fields clearly with that class.
<input name="veh<%=id%>" value="" type="text" id="veh<%=id%>" size="7" required="required" class="required" >
When I submit the form the hello alert comes up then form submits despite blank values.