Hello Universe
Hello Universe

Reputation: 3302

validate radio button with parsley.js

I am trying to validate radio button with parsley.js. Below is the code I am trying to use

  <p id="registration_field_custom2" class="onefield registration_field_custom2">
  <span class="doyou">
  <span class="text">etc etc etc etc ?</span>
    <input type="radio" name="custom2" value="Yes" id="custom2_yes" 
        data-required="true" 
        data-error-container="#submitDetails .error6" 
        data-error-message ="Please chose either yes or no"
    />
    <label for="custom2_yes">Yes</label>
    <input type="radio" name="custom2" value="No" id="custom2_no"  
    />
    <label for="custom2_no">No</label>     
  </span>
  </p>

However, no matter whether I chose yes or no, it still is looking for data required for both. I just want user to be able to chose either yes or no... how Do I do that with parsley.js?

Upvotes: 3

Views: 17190

Answers (3)

ken lun
ken lun

Reputation: 22

data-mincheck works for checkbox validation.

data-parsley-required="true" works for option validation

http://parsleyjs.org/doc/examples/simple.html

Upvotes: -2

Roman
Roman

Reputation: 1168

To validate radio button in Parsley, all you need to do is just use - data-required="true". You don't need to use data-mincheck for radio button. As the radio buttons have the same names, parsley automatically recognises that they are in the same group. There is good example in the Parsley documentation: http://parsleyjs.org/doc/index.html

Example:

<input id="radio-required" class="parsley-validated" type="radio" data-required="true" value="foo" name="radiorequired">
<input id="radio-required2" type="radio" value="bar" name="radiorequired">

Upvotes: 17

Knight
Knight

Reputation: 514

I think for radio input and checkbox is special case, in stead of using the the data-required , you should use the data-mincheck to ensure at least one of the option is checked.

Upvotes: 3

Related Questions