1

Is there a way to validate my script before submission, i.e pick up things as unfilled fields, incorrect emails etc

I am currently using expression engine's plugin freeform how it the plugin itself does not have any validation. Therefore i am trying to create a simple function myself.

I am receiving an error message saying the following

Uncaught TypeError: $(...).validate is not a function

below is a snippet of my code.

var form = $('#ajax-contact');
var formMessages = $('#form-messages');
$(form).submit(function (e) {
    e.preventDefault();
    var formData = $(form).serialize();
    // Submit the form using AJAX.
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: formData
    }).done(function (response) {
        if (response.success) {
            formMessages.removeClass('error').addClass('success').text("Thank you for submitting your details");
            $(".form-control").validate({
                rules: {
                    name: {
                        ranagelength: [2, 40],
                        required: true
                    },
                    email: {
                        ranagelength: [2, 40],
                        email: true,
                        required: true
                    },
                    errorClass: "error",
                    highlight: function (label) {
                        $(label).closest('.form-group').removeClass('has-success').addClass('has-error');
                    }
                }
            });
        } else {
            formMessages.removeClass("success").addClass("error").text("Oops, Please check your details");
        }
    }).fail(function (data) {
        // Make sure that the formMessages div has the 'error' class.
        $(formMessages).removeClass('success');
        $(formMessages).addClass('error');
        // Set the message text.
        if (data.responseText !== '') {
            $(formMessages).text(data.responseText);
        } else {
            $(formMessages).text('Oops! An error occured and your message could not be sent.');
        }
    });
});

1 Answer 1

1

You can try jquery validation valid() method.Refer this link and define your rule on ready event then only check like below.

$(document).ready(function () {
    $("#formid").validate({
        rules: {
            name: {
                ranagelength: [2, 40],
                required: true
            },
            email: {
                ranagelength: [2, 40],
                email: true,
                required: true
            },
            errorClass: "error",
            highlight: function (label) {
                $(label).closest('.form-group').removeClass('has-success').addClass('has-error');
            }
        }
    });
});

if ($("#formid").valid()) {
    //form is valid
} else {
    //forem is invalid
}
Sign up to request clarification or add additional context in comments.

3 Comments

would you be able elaborate on your answer a bit
i'm still slight confusion about your if formid.vaild section of the code
You can set .form-control class also but class may be duplicate so better to use id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.