Click here to Skip to main content
15,905,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi fellows,

I need your kind guidance regarding the following scenarios.

I have a section in my form which contains 1 textbox and 2 checkboxes.
I want to apply validation, that at one time only of the 3 should only be enabled and after unselecting them control could be transfered to another...

i.e. if we write some thing in checkbox then both checkboxes cant be checked and if we select any one checkbox than textbox and 2nd checkbox cant be filled.

I have applied validation on checkboxes but unable to apply on textbox completely.


problem is that when i write some thing in textbox than both textboxes got disabled but than can not be enabled again even if i do empty the textbox.

Kindly do help me in this regards.
Posted
Comments
[no name] 22-Mar-13 9:00am    
Help with what exactly? How would you expect us to know what it is that you have done when you do not post your code?
ZurdoDev 22-Mar-13 9:01am    
How can we help if we can't see the code?
VICK 25-Mar-13 6:04am    
Hi Guys.. there is a possibility some time, when code cant be pasted due to some reasons. That's y i tried to clear my problem through words.

1 solution

Hello,

I have come with a small prototype. Let me know if it works for you.
HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
function enforce(ctrl) {
    if (ctrl.id === "txtName") {
        $('#chkOne').prop("disabled", ctrl.value.length > 1);
        $('#chkTwo').prop("disabled", ctrl.value.length > 1);
    }
    else if (ctrl.id === "chkOne") {
        $('#txtName').prop("disabled", ctrl.checked);
        $('#chkTwo').prop("disabled", ctrl.checked);
    }
    else {
        $('#txtName').prop("disabled", ctrl.checked);
        $('#chkOne').prop("disabled", ctrl.checked);
    }
}
</script>
</head>
<body>
    <form id="frmMain" name="frmMain" method="post">
        <pre>
Name        : <input type="text" id="txtName" name="txtName" onChange="enforce(this);"/>
Nationality : <input type="checkbox"  id="chkOne" name="chkOne" value="O" onChange="enforce(this);"/>One
              <input type="checkbox"  id="chkTwo" name="chkTwo" value="T" onChange="enforce(this);"/>Two
         </pre>
    </form>
</body>
</html>


Regards,
 
Share this answer
 
Comments
VICK 25-Mar-13 6:03am    
Thanks Prasad for helping me out.. your post give me a gud idea to solve but problem was that i am using asp.net controls and they have different events against them. well finally googling more problem got solved. :)
Prasad Khandekar 25-Mar-13 6:30am    
I am glad that to know that your problem got solved. Even if you use asp.net controls on the client side it's still HTML and so the solution I proposed helps you do it without doing a postback.
VICK 26-Mar-13 3:48am    
ya u r ryt...but there is much difference in the events used by HTML controls and asp controls. for exmple onChange event which u mentioned in solution does not exist for asp checkbox..m i ryt???

actually m totally new to this field so concepts are nt cleard pretty much...so do correct if i m on mistake...thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900