radio buttons check validation in asp.net

Last post 10-26-2006 5:06 PM by SuperGhost. 1 replies.

Sort Posts:

  • radio buttons check validation in asp.net

    10-26-2006, 2:29 AM
    • Loading...
    • khushak
    • Joined on 08-21-2006, 9:21 AM
    • gurgaon, India
    • Posts 150

    hi

    in asp.net page three is three radio button and three text box. now  i have to check that if radio button one is selected then textbox1 is required to fill and text box 2 and 3 not required to fill , if radio button 2 selected then text 2 required to fill and text box 3 and 1 not required to fill and same for other 

    how i can alert messge and check this on button click

    regard

     

    New In .NET World
  • Re: radio buttons check validation in asp.net

    10-26-2006, 5:06 PM
    Answer
    • Loading...
    • SuperGhost
    • Joined on 12-29-2005, 4:58 PM
    • Palmdale CA, USA
    • Posts 404

    What you do is add this script to your page. Then add OnSubmit="return validate();" to your FORM tag.

    		<script language="javascript" type="text/javascript">
    			function validate()
    			{
    				var rdo = document.getElementById('<%=rdoList.ClientID%>');
    				var select = -1;
    				
    				if (rdo[0].checked) { select = 0; }
    				if (rdo[1].checked) { select = 1; }
    				if (rdo[2].checked) { select = 2; }
    			
    				switch (select)
    				{
    					case -1:
    						// No selection
    						alert('no selection'); return false;
    						break;
    					case 0:
    						// first item selected
    						var text1 = document.getElementById('<%=Textbox1.ClientID%>');
    						if (text1.value == '') { alert('Textbox 1 cannot be empty!!'); return false; }
    					case 1:
    						// second item selected
    						var text2 = document.getElementById('<%=Textbox2.ClientID%>');
    						if (text2.value == '') { alert('Textbox 2 cannot be empty!!'); return false; }
    					case 2:
    						// third item selected
    						var text3 = document.getElementById('<%=Textbox3.ClientID%>');
    						if (text3.value == '') { alert('Textbox 3 cannot be empty!!'); return false; }
    				}
    				
    				// if we get here then everything is valid
    			}
    		</script>
     
    ** Hope this helps!

    DennyDotNet.com
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter