SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Zealot samg914's Avatar
    Join Date
    Nov 2009
    Location
    Boston, MA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Onclick Validation

    I need to validate my form using this method:
    Code:
    onclick="MM_validateForm('first_name','','R','last_name','','R');return document.MM_returnValue"
    However, I also need this to work:
    Code:
    onclick="pageSwitch(2); top-of-page;"
    How can I put both of these into the same "Onclick" button?

    Much Thanks!

    Sam

  2. #2
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samg914 View Post
    Code:
    onclick="MM_validateForm('first_name','','R','last_name','','R');return document.MM_returnValue.
    and:
    onclick="pageSwitch(2); top-of-page;"
    onclick="bothFns()"

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

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

  3. #3
    SitePoint Zealot samg914's Avatar
    Join Date
    Nov 2009
    Location
    Boston, MA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AllanP View Post
    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?

    Code JavaScript:
    <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>

  4. #4
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That should do it.

  5. #5
    SitePoint Zealot samg914's Avatar
    Join Date
    Nov 2009
    Location
    Boston, MA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AllanP View Post
    That should do it.
    It doesn't seem to work for some reason. Did I write it properly?

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by samg914 View Post
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Zealot samg914's Avatar
    Join Date
    Nov 2009
    Location
    Boston, MA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Here is how that bothFns function should look:

    Code javascript:
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Zealot samg914's Avatar
    Join Date
    Nov 2009
    Location
    Boston, MA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Here is how that bothFns function should look:

    Code javascript:
    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...ation-help.php

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by samg914 View Post
    Immediately on the page there is another problem, because the "main-content-part-1" part of the page doesn't exist.
    Code:
    document.getElementById("main-content-part-1″).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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Zealot samg914's Avatar
    Join Date
    Nov 2009
    Location
    Boston, MA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Immediately on the page there is another problem, because the "main-content-part-1" part of the page doesn't exist.
    Code:
    document.getElementById("main-content-part-1″).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

  12. #12
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by samg914 View Post
    1. I need what the top-of-page part did to work
    Which is? Have you explained anywhere yet about what it did?

    Quote Originally Posted by samg914 View Post
    2. I need it to not show the second page until the required fields are entered.
    Was that ever able to work before merging things together?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  13. #13
    SitePoint Zealot samg914's Avatar
    Join Date
    Nov 2009
    Location
    Boston, MA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    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!

  14. #14
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by samg914 View Post
    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.
    Ahh, well you can use some proper scripting to achieve that.

    Code javascript:
    window.scrollTo(0, 0);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  15. #15
    SitePoint Zealot samg914's Avatar
    Join Date
    Nov 2009
    Location
    Boston, MA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Ahh, well you can use some proper scripting to achieve that.

    Code javascript:
    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.

  16. #16
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    The page switch needs to occur only if there are no errors, so:

    Code javascript:
    MM_validateForm('first_name','','R','last_name','','R');
    if (document.MM_returnValue) {
        // a validation error occurred
    } else {
        pageSwitch(2);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  17. #17
    SitePoint Zealot samg914's Avatar
    Join Date
    Nov 2009
    Location
    Boston, MA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The page switch needs to occur only if there are no errors, so:

    Code javascript:
    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.

  18. #18
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by samg914 View Post
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  19. #19
    SitePoint Zealot samg914's Avatar
    Join Date
    Nov 2009
    Location
    Boston, MA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    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.

  20. #20
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    I see in your code that you have this after the error alert.

    Code:
    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

    Code javascript:
    if (document.MM_returnValue === false) {
        // a validation error occurred
    } else {
        pageSwitch(2);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  21. #21
    SitePoint Zealot samg914's Avatar
    Join Date
    Nov 2009
    Location
    Boston, MA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    I see in your code that you have this after the error alert.

    Code:
    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

    Code javascript:
    if (document.MM_returnValue === false) {
        // a validation error occurred
    } else {
        pageSwitch(2);
    }
    This did the trick! Your help is greatly appreciated, Paul!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •