Livevalidation and group of checkbox, radio, selec

2019-06-25 10:01发布

Livevalidation script is great to validate forms but how you 're suppose to do with group of checkbox, radio, and select. see website : http://www.livevalidation.com/

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-25 10:14

I was having this same issue and came up with another way to look at it. I was using my radio group for acesses level -

  • Level one
  • Level two

So I just added a third level and set it to selected so that field is in essence required with a dummy value if you don't address the field.

  • Level one
  • Level two
  • no access
查看更多
Rolldiameter
3楼-- · 2019-06-25 10:22

For a select, you can use it just fine.

 <select name="foo" id="foo">
     <option value="" selected="selected"></option>
     <option value="value1">value1</option>
     <option value="value2">value2</option>
 </select>

But for radio buttons, it doesn't seem to work.

<input type="radio" name="bar" value="1" />
<input type="radio" name="bar" value="2" />

LiveValidation only works for elements that have an "id", which is not possible with radio buttons. Seems like a limitation of LiveValidation. Anyone has a workaround ?

查看更多
放荡不羁爱自由
4楼-- · 2019-06-25 10:29

It can be done, though LiveValidation will fight you. You can't attach a validator to an input type="radio".

So what I have done is add a dummy input element something like:

<input id="foo" type="text" style="display: none;" value="1" />

to my form, then add a Validate.Custom to that. The custom validation function would be something like

function () {
    return $('input[name=myRadioName]:checked').length;
}

This is what I did using the standalone LiveValidation and jQuery; probably can do something similar with prototype.js if you're using that version. One gotcha is that you must assign a value attribute to your dummy input element, because LiveValidation seems to skip inputs that have no value (excepting of course Validate.Presence).

查看更多
登录 后发表回答