Making Fields Required in this Google Forms Submitter Script
Hi, I came across the following script - and it's been awhile since I looked at and don't remember where I came up on it. GitHub I believe. At any rate, I was revisiting it and I need some help.
The form submissions work. When someone fills out the form results go to the Google Form as anticipated. However, none of the fields are currently required.
That's what I need help with. Where in the following script would I check if certain fields were filled out and report back to the user on submit? And what would be the template code to use to accomplish this?
None of the validEmail functionality that's currently in the script seems to do anything as I can put 'blah' in the email field and it submits w/o error.
Thanks!
- (function() {
- function validEmail(email) {
- var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
- return re.test(email);
- }
- function validateHuman(honeypot) {
- if (honeypot) { //if hidden form filled up
- console.log("Robot Detected!");
- return true;
- } else {
- console.log("Welcome Human!");
- }
- }
- // get all data in form and return object
- function getFormData(form) {
- var elements = form.elements;
- var fields = Object.keys(elements).filter(function(k) {
- return (elements[k].name !== "honeypot");
- }).map(function(k) {
- if(elements[k].name !== undefined) {
- return elements[k].name;
- // special case for Edge's html collection
- }else if(elements[k].length > 0){
- return elements[k].item(0).name;
- }
- }).filter(function(item, pos, self) {
- return self.indexOf(item) == pos && item;
- });
- var formData = {};
- fields.forEach(function(name){
- var element = elements[name];
-
- // singular form elements just have one value
- formData[name] = element.value;
- // when our element has multiple items, get their values
- if (element.length) {
- var data = [];
- for (var i = 0; i < element.length; i++) {
- var item = element.item(i);
- if (item.checked || item.selected) {
- data.push(item.value);
- }
- }
- formData[name] = data.join(', ');
- }
- });
- // add form-specific values into the data
- formData.formDataNameOrder = JSON.stringify(fields);
- formData.formGoogleSheetName = form.dataset.sheet || "responses"; // default sheet name
- formData.formGoogleSendEmail = form.dataset.email || ""; // no email by default
- console.log(formData);
- return formData;
- }
- function handleFormSubmit(event) { // handles form submit without any jquery
- event.preventDefault(); // we are submitting via xhr below
- var form = event.target;
- var data = getFormData(form); // get the values submitted in the form
- /* OPTION: Remove this comment to enable SPAM prevention, see README.md
- if (validateHuman(data.honeypot)) { //if form is filled, form will not be submitted
- return false;
- }
- */
- if( data.email && !validEmail(data.email) ) { // if email is not valid show error
- var invalidEmail = form.querySelector(".email-invalid");
- if (invalidEmail) {
- invalidEmail.style.display = "block";
- return false;
- }
- } else {
- disableAllButtons(form);
- var url = form.action;
- var xhr = new XMLHttpRequest();
- xhr.open('POST', url);
- // xhr.withCredentials = true;
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- xhr.onreadystatechange = function() {
- console.log(xhr.status, xhr.statusText);
- console.log(xhr.responseText);
- var formElements = form.querySelector(".form-elements")
- if (formElements) {
- formElements.style.display = "none"; // hide form
- }
- var thankYouMessage = form.querySelector(".thankyou_message");
- if (thankYouMessage) {
- thankYouMessage.style.display = "block";
- }
- return;
- };
- // url encode form data for sending as post data
- var encoded = Object.keys(data).map(function(k) {
- return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
- }).join('&');
- xhr.send(encoded);
- }
- }
-
- function loaded() {
- console.log("Contact form submission handler loaded successfully.");
- // bind to the submit event of our form
- var forms = document.querySelectorAll("form.gform");
- for (var i = 0; i < forms.length; i++) {
- forms[i].addEventListener("submit", handleFormSubmit, false);
- }
- };
- document.addEventListener("DOMContentLoaded", loaded, false);
- function disableAllButtons(form) {
- var buttons = form.querySelectorAll("button");
- for (var i = 0; i < buttons.length; i++) {
- buttons[i].disabled = true;
- }
- }
- })();