SaNmm
SaNmm

Reputation: 201

How to stop Onsubmit in Google forms

In Google forms i have written a validation check which checks for a field and if it exceeds a count then i need to stop the form submission and alert user about the error. I am able to add the error text on to the div but cant stop the form submission.

How do i stop the form submission. I have tried e.preventDefault() but it didn't work.can anyone please help. I have attached the code which is called on the event of form submit in google forms.

function onFormSubmit(e) {
  Logger.log("test log", "testing");
var timestamp = e.values[0];
var programname = e.values[1];
var center = e.values[2];
var fullname = e.values[3];
var board = e.values[4];
var programtype = e.values[5];
var programobjective = e.values[6];
var calltoaction = e.values[7];
var begindate = e.values[8];
var enddate = e.values[9];
var marketing = e.values[10];
var budget = e.values[11];
var expectedparticipation = e.values[12];
var registrationfee = e.values[13];
var announcement = e.values[14];
var insightannouncement = e.values[15];
var additionallanguage = e.values[16];
var checklist = e.values[17];
var speakerprofile = e.values[18];
var noticeboarditem = e.values[19];
var toAddress = e.values[20];
var programcategory = e.values[21];
var liability = e.values[22];
var datesofannouncement = e.values[23];
var explaination = e.values[24];
var additionalquestion = e.values[25];
var location = e.values[26];
var externalpartners = e.values[27];
var intergenerational = e.values[28];
var announcementlanguage = e.values[29];

 // var app = DocumentApp.getUi();

  var app = UiApp.getActiveApplication();

  Logger.log("test log in checkAnnouncement: " + announcement + " insight " + insightannouncement, "testing");
 // var announcement = e.values[14];
  //var insightannouncement = e.values[15]; 
  //var toAddress = e.values[20];
  //var htmlBody = "Thank you for your <b>Program Package</b> report submitted on <i>";
  //var subject = "test subject";
  Logger.log(announcement, "testing");
  Logger.log(insightannouncement, "testing");
 // Logger.log(toAddress, "testing");

  announcement = announcement.replace(/(^\s*)|(\s*$)/gi,"");
  announcement = announcement.replace(/[ ]{2,}/gi," ");
  announcement = announcement.replace(/\n /,"\n");

  insightannouncement = insightannouncement.replace(/(^\s*)|(\s*$)/gi,"");
  insightannouncement = insightannouncement.replace(/[ ]{2,}/gi," ");
  insightannouncement = insightannouncement.replace(/\n /,"\n");

  if(announcement.split(' ').length > 3)
  {
     app.getElementById('entry_15').setText("*Announcement should be less than 30 words").setStyleAttribute("color", "#F00");
     e.preventDefault(); 
  }
  else if(insightannouncement.split(' ').length > 5)
  {
    app.getElementById('entry_32').setText("*insightAnnouncement should be less than 50 words").setStyleAttribute("color", "#F00");
   e.preventDefault(); 
  }

}

Upvotes: 2

Views: 3202

Answers (2)

Ryno
Ryno

Reputation: 165

Assuming the above answers are correct, and this event fires post-submit, they are technically correct, you can't stop the submission, but you can remove the Response from the form's collection (which should delete it, if I've understood that part of the API correctly.

Upvotes: 0

Mogsdad
Mogsdad

Reputation: 45740

Data validation for Google Forms is limited to post-submission, such as when your onFormSubmission trigger is called. At that point in time, there is no ability to interact with the Form UI.

The Event your trigger function receives is a FormResponse object, which does not contain a preventDefault() method.

References

Upvotes: 3

Related Questions