diff --git a/microsetta_interface/implementation.py b/microsetta_interface/implementation.py index d0f5862b..8ed1bcf6 100644 --- a/microsetta_interface/implementation.py +++ b/microsetta_interface/implementation.py @@ -874,7 +874,8 @@ def get_ajax_check_ffq_code(ffq_code): except: # noqa return_val = False - return return_val + # Convert to JSON for returning to JavaScript call + return json.dumps(return_val) def _associate_sample_to_survey(account_id, source_id, sample_id, survey_id): diff --git a/microsetta_interface/templates/nutrition.jinja2 b/microsetta_interface/templates/nutrition.jinja2 index 4648156c..e2b1234b 100644 --- a/microsetta_interface/templates/nutrition.jinja2 +++ b/microsetta_interface/templates/nutrition.jinja2 @@ -44,27 +44,29 @@ preventImplicitSubmission(form_name); preclude_whitespace('#ffq_code'); - $("form[name='" + form_name + "']").on('submit', function() { - document.getElementById("ffq_code_button").disabled = true; - }); - - // Initialize form validation on the registration form. - // It has the name attribute "registration" + // Validate the FFQ code registration form using + // the jQuery validation plugin. $("form[name='" + form_name + "']").validate({ // Specify validation rules rules: { - // The key name on the left side is the name attribute - // of an input field. Validation rules are defined + // The key name on the left side is the + // name attribute of an input field. + // Validation rules are defined // on the right side + // the remote get ajax call needs to + // return the string "true" for the + // form to validate and submit ffq_code: { required: true, remote: { url: "/check_ffq_code_valid", } }, - submitHandler: function (form) { - form.submit(); - } + }, + submitHandler: function (form) { + // prevent multiple submissions + document.getElementById("ffq_code_button").disabled = true; + form.submit() }, messages: { ffq_code: "{{ _('Your registration code is not in our system or has already been used. Please try again.') }}", @@ -216,7 +218,7 @@
- +