Textbox validation in javascript in asp.net

RadhikaVyas
RadhikaVyas used Ask the Experts™
on
HI
I want to validate textbox using javascript as per some radio button check property..I have two radiobuttons r1 and r2...Two sets of textboxes gets activated as per radio button checked......i want to validate that textboxes in javascript....Can anyone help me out immediately........
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Anuradha GoliSystems Development / Support Specialist

Commented:
Just implemented sample code to validate length of textboxes
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        function chkradio1() {
            var radios = document.getElementsByName('preferred_color');
            if (radios[0].checked == true) {
                document.getElementById("Text2").disabled = true;
                document.getElementById("Text1").disabled = false;
            }
            else {
                document.getElementById("Text1").disabled = true;
                document.getElementById("Text2").disabled = false;
            }
        }
        function validate(text) {
            if (text.length > 10) {
                alert("Length more than 10 characters");
            }
        }
    </script>
</head>
<body>
    <p>
        <b>validations:</b></p>
    <input type="radio" name="preferred_color" value="one" onclick="chkradio1();" />
    one<br />
    <input type="radio" name="preferred_color" value="two" onclick="chkradio1();" />
    two<br />
    <input id="Text1" type="text" disabled="disabled" onkeydown="validate(this.value);" />
    <input id="Text2" type="text" disabled="disabled" onchange="validate(this.value);" />
</body>
</html>

Open in new window

Commented:
See example code below:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script  language=JavaScript type=text/javascript>
 function validate() {
   if(document.getElementById('gp1').checked) {
     document.getElementById('form').text1.disabled = false;
     document.getElementById('form').text2.disabled = false;
   }else{
     document.getElementById('form').text1.disabled = true;
     document.getElementById('form').text2.disabled = true;
   }
   if(document.getElementById('gp2').checked) {
     document.getElementById('form').text3.disabled = false;
     document.getElementById('form').text4.disabled = false;
   }else{
     document.getElementById('form').text3.disabled = true;
     document.getElementById('form').text4.disabled = true;
   }
}
</script>
</head>
<body>
<form id="form" name="form" action="tech/login_auth.asp" method="post">
TextBox Group 1
<br>
<input type="text" id="text1" name="text1">
<br>
<input type="text" id="text2" name="text2">
<br>
<input type="checkbox" id="gp1" name="gp1" onclick = "validate();">
Check To Enable Group 1
<br>
<br>
TextBox Group 2
<br>
<input type="text" id="text3" name="text3">
<br>
<input type="text" id="text4" name="text4">
<br>
<input type="checkbox" id="gp2" name="gp2" onclick = "validate();">
Check To Enable Group 2
<br>
<br>
</form>
</body>
</html>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial