I've created a form using Google Docs which sends data to a google spreadsheet. I've taken the html code and reformatted it to my css/style and it works perfectly. It also displays a custom thankyou page and the only problem I'm facing is that it doesn't validate the data.
So, I'm thinking of disabling the submit button if the user hasn't entered anything in the required fields. Or, you could suggest a way to prevent blank form submission?
Currently My form has 2 text fields, and 4 checkboxes. I require both the text fields, but not the checkboxes. The text fields are currently populated/cleared with html/script:
value="Your Email or Phone No."
onfocus="if (this.value=='Your Email or Phone No.') this.value='';"
I require my form to disable the submit button if no data has been entered.
Here's my full code:
<script type="text/javascript">var submitted=false;</script>
<iframe name="hidden_iframe" id="hidden_iframe" style="display:none;" onload="if(submitted) {window.location='thankyou.htm';}"></iframe>
<form action="Google_Form_ID_Here_(removed-for-stackoverflow-post)" method="POST" target="hidden_iframe" onsubmit="submitted=true;">
<ol style="padding-left: 0">
<div dir="ltr"><label for="entry_145">Name
<label for="itemView.getDomIdToLabel()" aria-label="(Required field)"></label>
</label>
<input type="text" name="entry.145" value="Enter Full Name" id="entry_145" dir="auto" aria-required="true" onfocus="if (this.value=='Enter Full Name') this.value='';">
</div>
<div dir="ltr" ><label for="entry_624">Contact Info
<label for="itemView.getDomIdToLabel()" aria-label="(Required field)"></label>
</label>
<input type="text" name="entry.624" value="Your Email or Phone No." id="entry_624" dir="auto" aria-required="true" onfocus="if (this.value=='Your Email or Phone No.') this.value='';">
</div>
<div align="center">
<div dir="ltr"><label for="entry_755"><br>Checkboxes
</div><br>
<div dir="ltr">Check which of the functions you'll be attending.</label><br><br>
<table align="center" width="248" height="128" border="0">
<span>
<tr>
<td align="center"><input id="group_418_1" name="entry.418" type="checkbox" value="Event 1"/>
<label class="vis_hide">Event 1</label></td>
<td align="center"><input id="group_418_2" name="entry.418" type="checkbox" value="Event 2"/>
<label class="vis_hide">Event 2</label></td>
<td align="center"><input id="group_418_3" name="entry.418" type="checkbox" value="Event 3"/>
<label class="vis_hide">Event 3</label></td>
<td align="center"><input id="group_418_4" name="entry.418" type="checkbox" value="Event 4"/>
<label class="vis_hide">Event 4</label></td>
</tr>
</span>
</table>
<input type="hidden" name="draftResponse" value="[]">
<input type="hidden" name="pageHistory" value="0">
<div dir="ltr">
<input type="submit" name="submit" value="Confirm" class="sbutton">
</div></ol></form>
Please can someone provide help regarding this? Or a suggestion for alternative to prevent blank submissions or provide a link to the solution? Can this be done with some jquery script too?
Thanks for any help.
EDIT:
I tried this script but that didn't help too...
<script>
$(function () {
// give the <form> the ID "myform" (or whatever you want) before using this code!
var form = $("#myform");
var input1 = $("#entry_145"); //for name
var input2 = $("#entry_624"); //for contact
form.addEventListener("submit", function (evt) {
if (input1.val() == "" || input2.val() == "") {
evt.preventDefault();
}
}, false);
});
</script>