0

i want to validate input during form submit for textbox B if it is enabled by radio 2. The input validation for textbox B is not working with the code below:

<script type="text/javascript">
$(document).ready(function() 
{
    $("#form").validate(
    {
        rules: {B: "required",}
    }); 

   ​$(':radio').change(function() 
   {
        $('#B').prop('disabled', !(this.value == '2' && this.checked));
   })​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​;         
});
</script>

<form id="form" name="form" method="post" action="">
<table><tr>
<td width><input type="radio" name="A" id="1" value="1" />1</td>
<td width><input type="radio" name="A" id="2" value="2" />2</td>
</tr></table>
<input name="B" type="text" id="B" disabled="disabled"/>
<input type="submit" name="form" id="Submit" value="Submit" />
</form>
2
  • 1
    Can you make a jsfiddle so we can see it in action? Commented Dec 12, 2012 at 3:01
  • @Xymostech, here it is: jsfiddle.net/newbie_85/R6qDa . But it is somehow not working. i'd just created my account in jsfiddle. Commented Dec 12, 2012 at 3:15

1 Answer 1

1

Try this,

$(document).ready(function() 
{
    $("#form").validate({
            rules: {
                B: "required",
            }
        });
    $('input:radio[name="A"]').change(
    function(){
        if ($(this).is(':checked') && $(this).val() == '2') {
            $('#B').removeAttr('disabled');
        }else if($(this).is(':checked') && $(this).val() == '1'){
            $('#B').attr('disabled', 'disabled');
        }
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

it's working now... But after i select the radio 2, then select back to radio 1, the textbox B is still enable. how to make it to be disabled again when radio 1 is selected?
you haven't mentioned that in your question that you want it disabled back
I have added the necessary code into the current answer try it out
Your are very helpful. Thanks for your effort ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.