SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Las Vegas, NV
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Validating a form field if radio button is checked

    Hi...

    I am having difficulties with this issue.

    I have a form that makes visible a text field when a specific radio button is checked. That works. Then I validate to see if a radio button is checked before I let the form be submitted. That works.

    BUT - I cannot seem to get it to validate the form field that is made visible to see if it has content or at least the proper content when the form is submitted. Can you tell me what I am doing wrong please?

    I can give more info on what kind of validation I want done if needed, but I think my code speaks for itself

    Code follows:
    Code:
    <script language="JavaScript"> 
    
    function validateForm(){ 
    
    var radio_choice = false; 
    
    for (counter = 0; counter < contact.respond.length; counter++) 
    { 
    if (contact.respond[counter].checked) 
    radio_choice = true; 
    } 
    
    if (!radio_choice) 
    { 
    alert("Please Select A way To Get In Contact With You"); 
    return (false); 
    } 
    
    
    if (document.contact.respond[0].checked) 
    { 
    if (document.contact.email == "No account email specified") 
    { 
    alert("Please update your email in your account info before submitting this form"); 
    document.contact.email.focus(); 
    return false; 
    } 
    } 
    
    if (document.contact.respond[1].checked) 
    { 
    if (document.contact.email == "") 
    { 
    alert("Please enter a phone number that you can be reached at"); 
    document.contact.telephone.focus(); 
    return false; 
    } 
    } 
    
    
    return true; 
    } 
    </script> 
    
    
    <form name="contact" action="contact_send.asp" Method="post" onSubmit="return validateForm()"> 
    
    Respond by: 
    
    <input type="radio" class="menumain" name="respond" value="email" onClick="document.getElementById('container1').style.visibility='visible';document.getElementById('container2').style.visibility='hidden';"> Email &nbsp;&nbsp; 
    
    <input type="radio" class="menumain" name="respond" value="telephone" onClick="document.getElementById('container1').style.visibility='hidden';document.getElementById('container2').style.visibility='visible';"> Telephone 
    
    <DIV id="container1"> 
    <input type="text" size="25" class="menumain" name="email" value="<%if session("email") = "" then%>No account email specified<%else%><%=session("email")%><%end if%>" readonly> 
    </DIV> 
    
    <DIV id="container2"> 
    <input type="text" size="25" class="menumain" name="telephone" value=""> 
    </DIV> 
    
    <input type="submit" value="Send" class="button"></form>
    >_< Please Help me!

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if (document.contact.email == "No account email specified")

    needs to be

    if (document.contact.email.value == "No account email specified")


    and same for the phone one.

  3. #3
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Las Vegas, NV
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    omg thank you so much for your help! I cant believe I left that out. it works like a charm now.
    U are a genius!
    I guess I should read thru my code a bit more closely before I give up and ask for help.

    u rock! thanks!

    Glenn

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no worries, it was a simple but annoying mistake that I have made many many times, after half an hour of debugging it's like *smacks forehead* DOH!

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my computer crashed...I could have been somebody.


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
  •