0

I have a form that I have set up to POST using Ajax. Currently my validation is basically this function:

// Function for making all fields required
$(function(){
    $("input").prop('required',true);
});

This works fine, however since parts of my form are hidden using CSS depending on previous answers selected - I get the following error for all hidden fields in the console:

An invalid form control with name='providerName' is not focusable.

Is there an easier way to add validation to an Ajax form, perhaps with more control?

E.g. some fields require a link - I think I can validate that the input has a 'link structure'

Example of my Ajax POST code:

// Wait for the DOM to be loaded 
$(document).ready(function() {

    // Make sure New Provider is unchecked by default   
    document.getElementById("typeNew").checked = false;

    // Variable to hold request
    var request;

    // Bind function to form submit event
    $("#createPanelForm").submit(function(event){

        // Abort any pending request
        if (request) {
            request.abort();
        }

        // Setup local variable
        var $form = $(this);

        // Select and cache form fields
        var $inputs = $form.find("projectName, projectLink, type, providerName, completeLink, quotaFullLink, screenoutLink, existingProvider");

        // Serialize the data in the form
        var serializedData = $form.serialize();

        // Disable the inputs for the duration of the Ajax request
        // Disabled form inputs will not be serialized.
        $inputs.prop("disabled", true);

        // Send the request
        request = $.post({
            url: "actions/createpanel.action.php",
            method: "POST",
            data: serializedData,
            function(result){
                alert(result);
            }
        });

        // Callback handler for successful request
        request.done(function (response, textStatus, jqXHR){
            // Log data to console
            console.log(serializedData);
            console.log(response);
            document.getElementById('submitSuccess').style.display = 'block';
        });

        // Callback handler for failed request
        request.fail(function (jqXHR, textStatus, errorThrown){
            // Log the error to console
            console.error("Error: "+textStatus, errorThrown);
            document.getElementById('submitError').style.display = 'block';
        });

        // Callback handler for both outcomes
        request.always(function () {
            // Reenable the inputs
            $inputs.prop("disabled", false);
        });

        // Prevent default form posting
        event.preventDefault();

        // Hide form and show response
        $("#createPanelForm").fadeOut(1000);
        $("#submitResponse").delay(1001).fadeIn(1000);      
    });

})
d.abyss
  • 204
  • 1
  • 4
  • 26
  • Use jQuery validation plugin https://jqueryvalidation.org/ – Mohit Bhardwaj Jul 06 '16 at 07:43
  • 1
    Possible duplicate of http://stackoverflow.com/questions/30644606/an-invalid-form-control-with-name-is-not-focusable-without-any-required-or-h – Anees Saban Jul 06 '16 at 07:49
  • @AneesSaban Checked out the link but doesn't seem entirely relevant to my question. I'm more interested in doing proper validation than relying on required attribute – d.abyss Jul 06 '16 at 08:22

1 Answers1

0

Adding a novalidate attribute to the form will help:

<form name="myform" novalidate>
Saravanan Sampathkumar
  • 3,201
  • 1
  • 20
  • 46