3

So I have a validation script I made a while ago. I can pass the script "this" as the form identifier to get it going on the right element (with(this)). But what if I want to call this validation function from a place other than the form itself, let's say, in an another request that wants to know if the form is valid?

   function validate_form(thisform) {
        with (thisform) {
            validate_fullName(fullName);
            validate_email(email);
            if (isName&&isEmail) {return true;};
            return false;
        };
    };

So basically I want to call this function in another function to check if the form is valid or not:

if(validate_form(WHATDOIPUTHERE?)){ // STUFF; };

Thank you so much!!

2 Answers 2

2

you can use document.getElementById("formId") (or $("#formId") in jquery):

if(validate_form(document.getElementById("formId"))){ // STUFF; };

For this to work you need to set the id of the form, like so:

<form id="formId" method="post" action="...">

</form>
Sign up to request clarification or add additional context in comments.

Comments

1

One way is to use id:

function validate_form('formid') {
    with ('formid') {
        validate_fullName(fullName);
        validate_email(email);
        if (isName&&isEmail) {return true;};
        return false;
    };
};


if(validate_form('formid')){ // STUFF; };

6 Comments

Fix your function definition.
@Matthew Flaschen: could not get that?
It should be function validate_form(formid) { with (formid) { No quotes
But this still doesn't quite make sense, because with expects an object, not an id.
@Matthew Flaschen: now i would simply ask, what is the solution in your eyes?
|

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.