textbox only accept number when first radio button checked

568 views javascript
4

i have four radio button, i want when user click first radio button, input textbox can only accept number but if they select other radio button, number and text only can be accepted. Below is my code :

<input class="form-control" type="text" id="usr_matriculation" maxlength="40" name="usr_matriculation" required="required">

    <div class="radio">
        <label class="radio-inline"><input type="radio" onclick="il.Form.showSubForm('subform_usr_id_type_1', 'usr_id_type', null);" name="usr_id_type" id="usr_id_type_1" value="1">
        New Identity Card No.</label>

    </div>


    <div class="radio">
        <label class="radio-inline"><input type="radio" onclick="il.Form.showSubForm('subform_usr_id_type_2', 'usr_id_type', null);" name="usr_id_type" id="usr_id_type_2" value="2">
        Passport No.</label>

    </div>


    <div class="radio">
        <label class="radio-inline"><input type="radio" onclick="il.Form.showSubForm('subform_usr_id_type_3', 'usr_id_type', null);" name="usr_id_type" id="usr_id_type_3" value="3">
        Army No.</label>

    </div>


    <div class="radio">
        <label class="radio-inline"><input type="radio" onclick="il.Form.showSubForm('subform_usr_id_type_4', 'usr_id_type', null);" name="usr_id_type" id="usr_id_type_4" value="4">
        Police No.</label>

    </div>

i try use below code but when i select other radio button, i cannot type text need to put number only. do i need to put if else? i need it auto change without refresh the page.

$('#usr_id_type_1').click(function() {
      alert("1");

    $('input[name="usr_matriculation"]').keyup(function(e)
                                    {
      if (/\D/g.test(this.value))
      {
        // Filter non-digits from input value.
        this.value = this.value.replace(/\D/g, '');
      }
    });
    });

answered question

Just remove the listener when the others are clicked. api.jquery.com/off

does it work with radio button?

1 Answer

8

When you click the first button that it add an event look for keyup of the $('input[name="usr_matriculation"]'), so every time you can not type text need to put number only.

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories