SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Form validation

  1. #1
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Ottawa
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form validation

    I have the following script on a form that has 3 radio buttons as questions. I would link to add some error handling to it - it user does not select an option, I would like to show some text "Please select and option"
    Thanks

    <script>
    function redirect(oform){
    var href=null
    for (var i=0;i<oform.rdoption.length;i++)
    { if (oform.rdoption[i].checked)
    href=oform.rdoption[i].value
    }
    if (href !=null)
    document.location.href=href
    return false
    }
    </script>

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Try the following function:

    Code:
    function redirect(oform)
    {
      var radio = oform.elements['rdoption'];
      
      for( var i = 0; i < radio.length; i++ )
      {
        if( radio[i].checked == true )
        {
          window.location.href = radio[i].value; return;
        }
      }
      
      alert('You have to select an option!');
    }
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Ottawa
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    No Luck!

    The script work for validation part but it does not redirect to the radio button's value page even when the radio buttos are checked.

    Thanks!!!

  4. #4
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Est
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is simple validator example.

    If you want to make some field (text/password/radio) required then all you need to do is add title attribute with errormessage into tag.

  5. #5
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Ottawa
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    That won't work

    I could not see the same on the page. I am not sure how it would work if all option buttons are required. Because there are 4-5 option buttons for each question and only one has to be selected. Here is my full code:

    The only thing I need to add to the existing code is to add some error handling message saying "Please select an answer...."

    Javascript:

    <script>
    function redirect(oform){
    var href=null
    for (var i=0;i<oform.rdoption.length;i++)
    { if (oform.rdoption[i].checked)
    href=oform.rdoption[i].value
    }
    if (href !=null)
    document.location.href=href
    return false
    }
    </script>

    HTML Form:

    <form onSubmit='return redirect(this)'>
    <p><b>Which of the following is a good reason to avoid idling your car?</b> </p>
    <table width="400" border="0" cellpadding="0" cellspacing="0">
    <tr>
    <td width="25" valign="top"><input name=rdoption type='radio' value= 'Q1_W.htm'>
    </td>
    <td width="28" valign="top">A)</td>
    <td width="397" valign="top">Gasoline is costly.</td>
    </tr>
    <tr>
    <td valign="top"><input name=rdoption type='radio' value= 'Q1_W.htm'>
    </td>
    <td valign="top">B)</td>
    <td valign="top">Burning fuel has significant environmental impacts.</td>
    </tr>
    <tr>
    <td valign="top"><input name=rdoption type='radio' value= 'Q1_W.htm'>
    </td>
    <td valign="top">C)</td>
    <td valign="top">Fuel is derived from a non-renewable resource.</td>
    </tr>
    <tr>
    <td valign="top"><input name=rdoption type='radio' value= 'Q1_R.htm'>
    </td>
    <td valign="top">D)</td>
    <td valign="top">All of the answers are correct.</td>
    </tr>
    </table>
    <div align="center"></div>
    <p>
    <input type=submit value="Submit">
    </p>
    </form>


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
  •