0

First off, yes I know it's not the best kind of question. But I'm not really sure what I should be looking for to find references. So if someone would have a hint for that I'd be grateful as well. I guess it would be some kind of pattern/anti-pattern reference but I'm not sure what keywords to use to find the ones that are applicable.

What I'm currently facing is a form a user has to input data into and submit. Before the user submits the form I'd like to verify some data that requires a backend call. In order to do so (and block the submit) I'm using a jQuery and $.getJSON/$.ajax to fetch the data. I also verify it again later on when I process the data. It's just about giving the user feedback for his input.

I'm not sure whenever my current approach is a good one. I'm not sure how I would block the submit and verify the data I get from the call by using an async call or whenever I should do it this way at all. Another option would be to submit the form, verify the data and get the user back to the form with information on what is wrong. The downside to this would be that a "heavy" load is necessary.

  1. So what could I do to use an asynchronous AJAX call to block the submit until I have verified the data?
  2. Should I do this at all or should I rather just submit the form and verify/reject it before submitting it for processing?

Edit to make clear it's not a duplicate:

Here is the current function I'm using to check input values before submit. As you can see it would only abort/interrupt the submit if there is an error detected. I'm using $.ajax instead of $.getJSON as it's blocking. Typically this shouldn't have a huge impact as the query is rather quick and this probably would work "good enough" but I feel like it should be possible to make this better.

$("form").submit(function(event){
    abortSubmit = false;

    telephonePattern = new RegEx("^[0-9, ,\\-,+]*$")
    if(telephonePattern.test($("#inputTelephone").value) == false){
        abortSubmit = true;
        $("#helptext").text("The telephone field contains invalid numbers");
    }

    userExists = false;

    $.ajax({
        url: "<url>",
        dataType: "json",
        async: false,
        data: { user: $("#inputUsername").value }
    }).done(
        function(json){
            userExists = json;
        }
    )

    if(userExists.length > 0){
        abortSubmit = true;
        $("#helptext").text("That username already exists!");
    }

    if(abortSubmit){
        return false;
    }
});
Seth
  • 1,215
  • 15
  • 35
  • Possible duplicate of [How to prevent form from being submitted?](http://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted) – 4b0 Apr 19 '17 at 11:44
  • 1
    You could hide the submit button, check the values as the user types and show/enable the submit button when all backends requests are successful. Or you could make the entire submitting asynchronous by ajax. Or set some kind of variable. You have many options. Try to reach for the best user experience. – Daniel W. Apr 19 '17 at 11:45
  • Thank @Shree but I'm able to prevent the submit. it's more as Dan pointed out a user experience or best practice question. I know this isn't the best platform for this but I'm not sure where else I'd go with this. – Seth Apr 19 '17 at 12:31

1 Answers1

2
$('.foo').on('click', function(e) {
    var validated_form = validateForm($(this).closest('form'));

    if (!validated_form) {
        e.preventDefault();
        // display validation messages, tooltips, etc
    };
});

.foo being the submit button

I assume you have a frontend validation function (in this example, it'd be named validateForm) which takes in the form jquery object. In that case, it should return either true (if everything is ok) or false (if things aren't ok). If things aren't ok, it won't submit the form. If they are ok, it will submit the form

If you absolutely must do the validation on the backend, then you could still use the preventDefault and send the form via ajax, validate it, then send a response. If the form was ok, you could trigger a custom event for that and then automatically submit the form (by creating an event listener for that specific event beforehand). If the form wasn't ok, you could again trigger a custom event (a different one) and display the validation messages and whatnot.

l.varga
  • 851
  • 6
  • 14
  • You should always do. After all this is front end code. Disable JS or ditch the browser altogether and submit whatever you want. ;) The second bit is rather interesting and gave me some more ideas. – Seth Apr 19 '17 at 12:48
  • 1
    @Seth, I meant if you absolutely must do only backend validation and cannot validate things for UX purposes on FE. I agree that you should do it on backend as well, even after re-submit - in this case, if you disable JS, it'll submit the entire form to BE anyway. But some stuff can, again, for the purpose of UX, easily be validated on FE. After the actual post of the form, it should be validated on BE again. As for the second part - might be even cleaner to do it without the event triggers and listeners, just execute proper functions based on xhr status or data ;) depends on your code really. – l.varga Apr 19 '17 at 13:06