SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Radio button validation works, but still submits form on error

    I'm having trouble with this radio button validation form. It works well by validating my form, however it still submits the form after getting an error. I know the javascript just needs to know to go back to the form and not to submit the form. Can anyone help?

    Code:
    <script LANGUAGE="JavaScript">
    <!--
    function ValidateForm(form){
    ErrorText= "";
    if ( ( form.voteid[0].checked == false ) && ( form.voteid[1].checked == false ) ) { alert ( "Please make a poll selection." ); return false; }
    if (ErrorText= "") { form.submit() }
    }
    -->
    </script>
    
    
    onClick="ValidateForm(this.form)"

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your button is a submit button.

    use the following

    Code xhtml:
    <form onsubmit="return ValidateForm(this);">....</form>

    Code javascript:
    <!--
    function ValidateForm(form){
    ErrorText= "";
    if ( ( form.voteid[0].checked == false ) && ( form.voteid[1].checked == false ) ) { alert ( "Please make a poll selection." ); return false; }
    return true;
    }
    -->


  3. #3
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That seems to work great, although the message popup actually pops up again when you click 'Ok', then goes away. Any idea why this is coming up twice?

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have you still for form.submit() in your javascript?


  5. #5
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I did take it out.

  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the only other suggestion is you still have the onclick on the button?

    If not, could you post a url or pm me with an url that i can quickly take a look at?


  7. #7
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops! You're right. I had an onclick left over in the submit button. Works great now!

    Thanks a bunch! Really appreciate it.


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
  •