Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
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 22-Mar-13 3:51am
VICK8.5K
Comments
ThePhantomUpvoter at 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?
ryanb31 at 22-Mar-13 9:01am
   
How can we help if we can't see the code?
Khan Ji at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
I have come with a small prototype. Let me know if it works for you.
<!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,
  Permalink  
Comments
Khan Ji at 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 at 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.
Khan Ji at 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)

  Print Answers RSS
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 22 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100