0

I have an ajax form set up for my EE contact form which requires that the email and message field be filled in. I need to have "name" and "subject" required too. Please help

 // Contact form to use AJAX.
    $(document).ready(function() 
    {
        /* helper function to display a message describing the results of the form submit */
        function displayAjaxMessage(message)
        {
            $("#ajax-message").html(message);
            $("#ajax-message").fadeIn(1000);
        }

        /* attach a submit handler to the contact form. By default, EE generates the ID "contact_form" */
        $("#contact_form").submit(function(event) 
        {
            /* stop the contact form from submitting normally */
            event.preventDefault(); 

            /* hide any left over message from a previous submit */
            $("#ajax-message").hide();

            /* send the form data using post and check the results for any errors*/ 
            $.ajax(
            {
                url: "/",
                type: "post",
                dataType: "html",           
                data: $(this).serialize(),

                /* If there was some kind of an AJAX error, display an appropriate message or take some other action of your choice */
                error: function(jqXHR, textStatus, errorThrown)
                {
                    displayAjaxMessage("Sorry, a mistake in form");
                },

                /*  parse the HTML returned by EE to see if they forgot to enter an email address or a message. 
                    If so, the HTML will contain a specific error string we can match, and then we can display our own message */
                success: function(html, textStatus, jqXHR) 
                {
                    if (html.match(/<title>Error<\/title>/)) 
                    {
                        var error = $(html).find('ul li:first').text(); 
                        if (error == "A valid sender email is required")
                        {
                            displayAjaxMessage("Vennligst oppgi en gyldig mailadresse");
                        }
                        else if (error == "Email Message is Required")                  
                        {
                            displayAjaxMessage("Legg inn en melding");                      
                        }
                    }
                    else
                    {
                        displayAjaxMessage("Takk for at du kontakter oss");
                    }
                }

            });
        });
    });
1
  • Umm... obviously you are validating your fields in the back end somehow, so front end code is useless until we see what's going on behind the scenes. I don't even see this AJAXing an ACTion ID... I'm not sure this is even EE related.
    – jrothafer
    Commented Sep 10, 2015 at 12:58

2 Answers 2

1

Looks like you can add a name=required[] parameter to each form field you want to make required:

https://ellislab.com/expressionengine/user-guide/add-ons/email/contact_form.html#required

1

read much more in details in this site here - http://watabelabs.com/blog/view/submitting-expressionengines-contact-form-with-ajax

1
  • Consider providing the important detail from the link in your answer. Answers should continue to work as a resource even if that link goes dark.
    – UltraBob
    Commented Jul 14, 2016 at 0:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.