Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    406
    Thanks
    0
    Thanked 0 Times in 0 Posts

    form validation not working

    I created a form and validation, but it is not working. Actually the form has 2 radio buttons (Initiall none of them are selected). So I wrote a validation that user should select a radio button before submitting (I am using an image instead of submit button). Also I have to validate a text box to see the user enters some thing.

    When I was testing, if the user doesn't enter any thing in the radio buton, it shows an alert. When I click on the alert, I get a blank white page with "false" on it. Also even if I select a radio button, and leave the text box blank (validation for text box) it comes and says "Please select a radio button". I couldn't figure this out. Some one help me

    <html>
    <head>
    <title>blank</title>
    <script language="JavaScript">

    function validate()
    {

    var radio_choice = false;

    for (counter = 0; counter < userinput.radio_db; counter++)
    {
    if (userinput.radio_db[counter].checked)
    radio_choice = true;
    }
    if (!radio_choice)
    {
    alert("Please select a database.")
    return false;
    }
    if ((document.userinput.for1.value.length == 0) || (document.userinput.for1.value = null)){
    alert ('Please enter some search term in the text box');
    userinput.for1.focus();
    }
    return true;
    }
    </script>


    </head>

    <body bgcolor="#FFFFFF" topmargin="0" marginheight="0" marginwidth="0">

    <form name="userinput" action="#" method="post">
    <table>
    <tr>
    <td valign="top" bgcolor="#EFEFD6"><B>SELECT DATABASE</b></td>
    </tr>
    <tr>
    <td valign="top">
    <input type="radio" name="radio_db" value="lit"> Lit &nbsp; &nbsp; <input type="radio" name="radio_db" value="pat">Pat </td>
    </tr>
    <tr>
    </tr>
    <tr>
    <td valign="top" bgcolor="#EFEFD6"><b>SEARCH FOR</b></td>
    </tr>
    <tr>
    <td valign="top" align="right" bgcolor="#EFEFD6" ><input type="text" name="for1" size="29"></td></tr>
    <tr><td valign="top"><A href="javascript:validate();"><img src="images/search_orange.gif" border="0"></a></td></tr>
    </table>
    </form>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <html> 
    <head> 
    <title>blank</title> 
    <script language="JavaScript"> 
    
    function validate() 
    	{ 
    	var radio_choice = false; 
    	for (counter = 0; counter < document.userinput.radio_db.length; counter++) 
    		{ 
    		if (document.userinput.radio_db[counter].checked) 
    			radio_choice = true; 
    		} 
    	if (!radio_choice) 
    		{ 
    		alert("Please select a database.") 
    		return false; 
    		} 
    	if ((document.userinput.for1.value.length == 0) || (document.userinput.for1.value = null))
    		{ 
    		alert ('Please enter some search term in the text box'); 
    		userinput.for1.focus(); 
    		return false;
    		} 
    	document.userinput.submit(); 
    	} 
    </script> 
    
    
    </head> 
    
    <body bgcolor="#FFFFFF" topmargin="0" marginheight="0" marginwidth="0"> 
    
    <form name="userinput" action="#" method="post"> 
    <table> 
    <tr> 
    <td valign="top" bgcolor="#EFEFD6"><B>SELECT DATABASE</b></td> 
    </tr> 
    <tr> 
    <td valign="top"> 
    <input type="radio" name="radio_db" value="lit"> Lit     <input type="radio" name="radio_db" value="pat">Pat </td> 
    </tr> 
    <tr> 
    </tr> 
    <tr> 
    <td valign="top" bgcolor="#EFEFD6"><b>SEARCH FOR</b></td> 
    </tr> 
    <tr> 
    <td valign="top" align="right" bgcolor="#EFEFD6" ><input type="text" name="for1" size="29"></td></tr> 
    <tr><td valign="top"><A href="#" onclick="validate();return false;">Submit</a></td></tr> 
    </table> 
    </form> 
    </body> 
    </html>


  •  

    Posting Permissions

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