简体   繁体   English

想要使用magento验证来验证magento中的复选框

[英]Want to validate the check box in magento using magento validation

I want to set the required field for the check box i tried my coding 我想为我尝试编码的复选框设置必填字段

 <input class="checkbox required-entry" type="checkbox" name="checkbox[]" />
<input class="checkbox required-entry" type="checkbox" name="checkbox[]" />
<input class="checkbox required-entry" type="checkbox" name="checkbox[]" />
<input class="checkbox required-entry" type="checkbox" name="checkbox[]" />
<input class="checkbox required-entry" type="checkbox" name="checkbox[]" />

i check any one of the field and press submit button the remaining field shows "This is a required field" . 我选中任何一个字段,然后按提交按钮,其余字段显示“这是必填字段”。 I Want to set the required field in check box if i not check any one of the value the required message will show other wise if not mandatory 如果我未选中任何一个值,则我想在复选框中设置必填字段,如果不是强制性的,则必填消息将以其他方式显示

Thank You 谢谢

From Really Easy Field validation : 通过Really Easy Field验证

  • validate-one-required (At least one textbox/radio element must be selected in a group – see below*) 验证一个必填项(必须在一个组中至少选择一个文本框/无线电元素–参见下文*)

*To use the validate-one-required validator you must first add the class name to only one checkbox/radio button in the group (last one is probably best) and then place all the input elements within a parent element, for example a div element. *要使用需要验证一个的验证器,必须首先将类名添加到组中的一个复选框/单选按钮(最后一个可能是最好的),然后将所有输入元素放在父元素中,例如div元件。 That way the library can find all the checkboxes/radio buttons to check and place the validation advice element at the bottom of the parent element to make it appear after the group of checkboxes/radio buttons. 这样,库可以找到所有复选框/单选按钮进行检查,并将验证建议元素放在父元素的底部,以使其显示在复选框/单选按钮组之后。

Therefore you could do this: 因此,您可以这样做:

<fieldset>
    <input class="checkbox" type="checkbox" name="checkbox[]" />
    <input class="checkbox" type="checkbox" name="checkbox[]" />
    <input class="checkbox" type="checkbox" name="checkbox[]" />
    <input class="checkbox" type="checkbox" name="checkbox[]" />
    <input class="checkbox validate-one-required" type="checkbox" name="checkbox[]" />
</fieldset>

But since those checkboxes share the same name (this isn't documented but is in the source) you could also do: 但是,由于这些复选框具有相同的名称(未记录,但在源中),您也可以这样做:

<!-- Maybe these are columns and don't share a parent element -->
<div>
    <input class="checkbox" type="checkbox" name="checkbox[]" />
    <input class="checkbox" type="checkbox" name="checkbox[]" />
    <input class="checkbox validate-one-required-by-name" type="checkbox" name="checkbox[]" />
    <!-- This checkbox is lowest on the page so should have the message -->
</div>
<div>
    <input class="checkbox" type="checkbox" name="checkbox[]" />
    <input class="checkbox" type="checkbox" name="checkbox[]" />
</div>

在类中添加required-entry

<input type="checkbox" id="id" name="name" value="value" class="checkbox required-entry">

只要输入共享相同的名称,就可以使用: validate-one-required-by- name(通常是这种情况)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM