admin管理员组

文章数量:1294541

I'm using BootstrapValidator and I have some text fields and a radio button with 2 options, I need to validate a text field only if the radiobutton option 2 is selected, how can I obtain it?

This is the form:

<form action="/it/Account/SignUp" class="form-horizontal" id="signup_form" method="post" role="form">
<label>Name</label>
<input type="text" name="name" class="form-control" id="name">
<label>Company</label>
<input type="text" name="pany" class="form-control" id="pany">
<input type="radio" name="usertype" value="0"><label>Private</label>
<input type="radio" name="usertype" value="1"><label>Company</label>

</form>

This is the JavaScript for BootstrapValidator:

$('#signup_form').bootstrapValidator({
    message: 'This value is not valid',
    live: 'disabled',
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        name: {
            validators: {
                notEmpty: {
                    message: 'required'
                },
            }                
        },
        pany: {
            validators: {
                notEmpty: {
                    message: 'required'
                },
            }                
        },
    }
}

I need only to validate the "pany" field only if the radio value of 'usertype' is equal to 1

I'm using BootstrapValidator and I have some text fields and a radio button with 2 options, I need to validate a text field only if the radiobutton option 2 is selected, how can I obtain it?

This is the form:

<form action="/it/Account/SignUp" class="form-horizontal" id="signup_form" method="post" role="form">
<label>Name</label>
<input type="text" name="name" class="form-control" id="name">
<label>Company</label>
<input type="text" name="pany" class="form-control" id="pany">
<input type="radio" name="usertype" value="0"><label>Private</label>
<input type="radio" name="usertype" value="1"><label>Company</label>

</form>

This is the JavaScript for BootstrapValidator:

$('#signup_form').bootstrapValidator({
    message: 'This value is not valid',
    live: 'disabled',
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        name: {
            validators: {
                notEmpty: {
                    message: 'required'
                },
            }                
        },
        pany: {
            validators: {
                notEmpty: {
                    message: 'required'
                },
            }                
        },
    }
}

I need only to validate the "pany" field only if the radio value of 'usertype' is equal to 1

Share edited Dec 21, 2014 at 13:58 Zanon 30.8k21 gold badges118 silver badges126 bronze badges asked May 18, 2014 at 19:58 CaTouristCaTourist 7333 gold badges9 silver badges21 bronze badges 2
  • 1 Wele to Stack Overflow. Please provide an example of what you have tried so far, and the issues you encountered? You're more likely to receive a helpful response from the munity if you can demonstrate that you attempted to solve the problem before posting. – Alex Osborn Commented May 18, 2014 at 21:10
  • i inserted a code sample – CaTourist Commented May 19, 2014 at 9:01
Add a ment  | 

1 Answer 1

Reset to default 4

You should look at the callback validation option

https://formvalidation.io/guide/validators/callback

you can create a custom function for validation, return a boolean of true or false, you should then do your checks on the fieldValue depending on if the radio button is checked

本文标签: javascriptBootstrapValidator conditional validationStack Overflow