Onclick Validation

I need to validate my form using this method:

onclick="MM_validateForm('first_name','','R','last_name','','R');return document.MM_returnValue"

However, I also need this to work:

onclick="pageSwitch(2); top-of-page;"

How can I put both of these into the same “Onclick” button?

Much Thanks!

Sam

onclick=“bothFns()”

function bothFns(){ fn1(); fn2(); }

Create fn1() and fn2() so that they include the two scripts shown in the quote above.

Is this right?

<script type="text/javascript">
function bothFns()	{
	fn1("MM_validateForm('first_name','','R','last_name','','R');return document.MM_returnValue");
	fn2("pageSwitch(2); top-of-page;"); }
</script>

That should do it.

It doesn’t seem to work for some reason. Did I write it properly?

fn1() and fn2() are only placeholders, kind of like foo() and bar()

In other words, you’re supposed to replace them with the functions that you want to be run instead.

I have a very weak understanding of javascript. So I don’t really understand where the code is suppose to go and how it should be written.

Here is how that bothFns function should look:


function bothFns()  {
    // pageswitch
    pageSwitch(2);
    top-of-page;

    // validate
    MM_validateForm('first_name','','R','last_name','','R');
    return document.MM_returnValue;
}

There might be some compatibility issues with the return value though, so if you have any further issues, put a test page up on the internet and let us look at that instead.

Thanks! It now goes to the next page, but it does not validate the fields.

Here is a link to the page: http://www.searchtransparencysem.com/projects/isc/sitepoint-validation-help.php

Immediately on the page there is another problem, because the “main-content-part-1” part of the page doesn’t exist.

document.getElementById("main-content-part-1&#8243;).style.display = "block";  

Back with the validation, I don’t think that the top-of-page part was ever going to work. Remove that part, and see if that improves your ability to validate.

I removed the “top-of-page” part and now the validation works, but I have two problems now.

  1. I need what the top-of-page part did to work
  2. I need it to not show the second page until the required fields are entered.

Thanks

Which is? Have you explained anywhere yet about what it did?

Was that ever able to work before merging things together?

The “top-of-page” part just auto-scrolled to the top of the page when the user clicked the next or back button.

It didn’t need to work until I merged these functions together. I just don’t want the user to have to click back in order to fillout the required fields.

Thanks!

Ahh, well you can use some proper scripting to achieve that.


window.scrollTo(0, 0);

Thanks Paul! It’s starting to look good. Now I just need to know how to make it so that “page switch function” is only triggered if the required fields are filled out.

The page switch needs to occur only if there are no errors, so:


MM_validateForm('first_name','','R','last_name','','R');
if (document.MM_returnValue) {
    // a validation error occurred
} else {
    pageSwitch(2);
}

I have inserted this code into the page, but once the error box pops up and I click “OK” it continues to the next page still.

Are you sure the change has been published to the web? I don’t see the updated code on the test page that you linked to before.

Sorry, I had it on another copy. It’s uploaded now.

I see in your code that you have this after the error alert.

document.MM_returnValue = (errors == '');

What does that do to document.MM_returnValue ? We’ll have to think hard about this.

If there are any errors, then the condition will be false, and that will be assigned to document.MM_returnValue, which is not actually returned by the function but is instead stored in a global variable.

So, the if statement need to check if the return value is false


if (document.MM_returnValue === false) {
    // a validation error occurred
} else {
    pageSwitch(2);
}