1

I have this website currently and giving me problems because I am running two forms on the website, and jquery is getting confused about the validation because its just finding the INPUT, and since there is two forms it's getting an error because the input is blank in the other form...

How do I select a specific input in jquery instead of the universal input?

// Email Form
    required = ["fullName" , "emailAddress"];
    email = $("#emailAddress");
    errornotice = $("#error");
    emptyerror = "Required field.";
    emailerror = "Enter a valid e-mail.";

    $("#theformemail").submit(function(){   
        //Validate required fields
        for (i=0;i<required.length;i++) {
            var input = $('#'+required[i]);
            if ((input.val() == "") || (input.val() == emptyerror)) {
                input.addClass("needsfilled");
                input.val(emptyerror);
            } else {
                input.removeClass("needsfilled");
            }
        }
        // Validate the e-mail.
        if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
            email.addClass("needsfilled");
            email.val(emailerror);
        }

        //if any inputs on the page have the class 'needsfilled' the form will not submit
        if ($(":input").hasClass("needsfilled")) {
            return false;
        } else {
            $("#newOne").show().delay(2000);
            errornotice.hide();
            return true;
        }
    });

??????? Here is also the actual testing page of the website.

http://www.trento110.com/site/index.php

If you click on - private events there is one form.. then if you click on "send to phone on the botton.. there is the other form, and notice the conflict.

Thanks in advance.

5
  • Add a class to the inputs on either form..then select with $("input.formClass") Commented Aug 13, 2013 at 14:40
  • I tried that, but what about this section of the code? var input = $('#'+required[i]);... i get an error if I write it like var $("input.formClass") Commented Aug 13, 2013 at 14:43
  • It sounds a bit confusing. Do those two forms share the same ID? When you're validating one form, is the other one present at this same page? I ask this, because there's no way another form, in another page (which is not loaded) would be validated at the same time. Commented Aug 13, 2013 at 14:44
  • Yes, it is on the same page in a modal... they have different id's... Commented Aug 13, 2013 at 14:47
  • So the answer posted by @Neo should do the trick, if you remember to change the jQuery selector, which is pointing to a specific form ID. Commented Aug 13, 2013 at 14:49

1 Answer 1

2

please try this

$("#theformemail").submit(function(){   
        //Validate required fields
        for (i=0;i<required.length;i++) {
            var input = $('#'+required[i]); 

//change this line with

            var input =$(this).find('#'+required[i]);
Sign up to request clarification or add additional context in comments.

Comments

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.