Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript validation/prevent submit form amazon mechanical turk

I am using Amazon survey form template to create a survey.

however i would like to use validation.

I saw this question however i don't want to use the web services to create the hit but use their template.

How can I prevent the form from being submitted?

like image 608
Dejell Avatar asked May 10 '11 07:05

Dejell


2 Answers

I found a way to do it:

I added a function to the submit button:

<script type='text/javascript'>
window.onload = function() {document.getElementById('submitButton').setAttribute('onclick', 'return validateForm()'); }



function validateForm() {
if (validate)
return true;
else
return false;
}
</script>
like image 82
Dejell Avatar answered Sep 30 '22 15:09

Dejell


Here's an example of doing the validation with jQuery

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
  $('#submitButton').click(function() {
    if ($('input[name=myField]').val() == '') {
      alert('myField is required.');
      return false;
    }
    return true;
  });
});
</script>
like image 22
Dolan Antenucci Avatar answered Sep 30 '22 16:09

Dolan Antenucci