简体   繁体   English

如何验证 Materializecss 单选按钮?

[英]How to validate Materializecss radio buttons?

I tried to implement validation to display an error message on radio button but I couldn't succeed.我试图实现验证以在单选按钮上显示错误消息,但我无法成功。

I'm using Materializecss framework.我正在使用Materializecss框架。

The validation/displaying error message works on text box element and not in radio button.验证/显示错误消息适用于文本框元素,而不适用于单选按钮。

Code:代码:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!-- Compiled and minified CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css"> <!-- Compiled and minified JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/js/materialize.min.js"></script> <form> <div class="row"> <div class="input-field col s6"> <input class="validate" type="radio" name="mode" value="M" id="m" required="required" data-error="Error msg here"> <label for="m">M</label> </div> <div class="input-field col s6"> <input class="validate" type="radio" name="mode" value="F" id="f" required="required" data-error="Error msg here"> <label for="f">F</label> </div> <div class="col s12" style="margin-top:20px"> <button type="submit" class="waves-effect waves-green btn"> Submit </button> </div> </div> </form>

Why the error message is not displaying.为什么不显示错误消息。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks.谢谢。

If you can use jQuery, you could implement a solution based on this fiddle .如果您可以使用 jQuery,您就可以实现基于这个 fiddle的解决方案。

By placing the group of radio buttons in a container div with class radioRequired :通过将单选按钮组放置在具有类radioRequired的容器 div 中:

<div class="col s12 radioRequired" name="mode" data-error="Error msg here">

JQuery can be used to check if there are any selected radio buttons with the name specified in the container. JQuery 可用于检查是否有任何选定的具有在容器中指定的名称的单选按钮。 If there are none, highlight the div and add an error message with class radioWarning and text based on the data-error setting in the container.如果没有,突出显示 div 并根据容器中的数据错误设置添加带有类radioWarning和文本的错误消息。 If a selection has been made, remove the highlight and the error message.如果已做出选择,则删除突出显示和错误消息。

The radio buttons can be specified as:单选按钮可以指定为:

  <input type="radio" name="mode" value="M" id="m">
  <label for="m">M</label>

Finally, if there are radioWarning s, prevent the default Submit action otherwise allow it as normal.最后,如果有radioWarning s,请阻止默认的 Submit 操作,否则照常允许。

I have worked with this framework and there is nothing to do with radio button form validation by default.我使用过这个框架,默认情况下与单选按钮表单验证无关。 I used following structure in order to add validation to my radio buttons.我使用以下结构来向我的单选按钮添加验证。

I added a class name "radio" to input parents and a script before closing body tag.在关闭 body 标签之前,我添加了一个类名“radio”来输入父项和脚本。

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!-- Compiled and minified CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css"> <!-- Compiled and minified JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/js/materialize.min.js"></script> <form> <div class="row"> <div class="radio input-field col s6"> <input class="validate" type="radio" name="mode" value="M" id="m" required="required" data-error="Error msg here"> <label for="m">M</label> </div> <div class="radio input-field col s6"> <input class="validate" type="radio" name="mode" value="F" id="f" required="required" data-error="Error msg here"> <label for="f">F</label> </div> <div class="col s12" style="margin-top:20px"> <button type="submit" class="waves-effect waves-green btn"> Submit </button> </div> </div> </form> <script> $('.radio lable, .radio input').click(function(){$(this).parent().siblings().removeClass('checked'); $(this).parent().addClass("checked")}); $("form button[type=submit]").click(function(e){ e.preventDefault(); $("form input:radio").each(function() { if ($(this).attr('required')){ if(!$(this).parent().hasClass('checked')){ //some code for when nothing is chosen console.log("nothing is chosen"); } else{ console.log("value of checked radio button is: "+$(this).val()); } } else { console.log("not required"); } }); }); </script>

I hope it solves the issue.我希望它能解决问题。

https://jqueryvalidation.org/ Use this plugin. https://jqueryvalidation.org/使用这个插件。 Jquery Validation. jQuery 验证。 It worked for me.它对我有用。 It is well documented and easy to use.它有据可查且易于使用。

You put some rules, then call out the validate function from that plugin.您放置一些规则,然后从该插件中调用验证功能。

I'm using materializes for frontend and to validate the buttons radio use jquery.validate.js and in the change the lines我正在为前端使用物化并验证按钮无线电使用 jquery.validate.js 并在更改行中

elementID = this.idOrName( elemnt ), for elementID = $( elemnt ).attr("id"), elementID = this.idOrName( elemnt ), for elementID = $( elemnt ).attr("id"),

and comment on this line this.addWrapper( errors ).hide();并在这一行注释this.addWrapper( errors ).hide();

A quick research on materialize.css reveals that this framework's style is missing proper selectors for an input of type radio , check out this image:materialize.css的快速研究表明,该框架的样式缺少用于radio类型的输入的正确选择器,请查看此图像:

在此处输入图片说明

Following code contains styling of :after selectors for labels.以下代码包含:after标签选择器的样式。 However, adding a selector for radio inputs doesn't seem to be fixing the problem for me.但是,为无线电输入添加选择器似乎并没有解决我的问题。

To fix your issue, you can either:要解决您的问题,您可以:

  • Make a form validator function which checks the radios value and alerts the user, then hook this function to a form's onsubmit event.制作一个表单验证器函数来检查无线电值并提醒用户,然后将此函数挂接到表单的onsubmit事件。
  • Attach a validator function to a submit button, which will artificially prevent the button from being clicked and the form from being submitted.将验证器函数附加到提交按钮,这将人为地阻止按钮被点击和表单被提交。

Here's a more cleaner approach.这是一种更清洁的方法。

I guess developers of this framework excluded the code for radiobutton validation intentionally.我猜这个框架的开发人员有意排除了单选按钮验证的代码。 Radio button is a common UI element which comes in radio button groups .单选按钮是一个常见的 UI 元素,它出现在单选按钮组中 Radio button is specific in a way there can be only one selected value in a group, and there must be a selected value .单选按钮的特定方式是一组中只能有一个选定的值,并且必须有一个选定的值

So, change your M input from this:因此,从这里更改您的 M 输入:

<input class="validate" type="radio" name="mode" value="M" id="m" required="required" data-error="Error msg here">

To this:对此:

<input checked required class="validate" type="radio" name="mode" value="M" id="m">
  • HTML5 doesn't require a value for specific modifiers such as required , disabled , selected so I removed it. HTML5 不需要特定修饰符的值,例如requireddisabledselected所以我删除了它。
  • Removed unused data-error attribute.删除了未使用的data-error属性。
  • I moved modifiers closer to the beginning so you see those at a glance.我将修饰符移到更靠近开头的位置,以便您一目了然。
  • I added a checked modifier so the radio group has a default value.我添加了一个checked修饰符,所以无线电组有一个默认值。

It appears you're making a gender selector.看来您正在制作性别选择器。 There are only two genders and you always have a gender, there's no reason to not have a default value.只有两种性别而且你总是有一个性别,没有理由没有默认值。 In terms of UI/UX design a radio button group should always have a checked element.在 UI/UX 设计方面,单选按钮组应该总是有一个选中的元素。 If you want to, you might include third option if the user doesn't want to provide this info.如果您愿意,如果用户不想提供此信息,您可以包含第三个选项。

Here's your updated snippet:这是您更新的片段:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!-- Compiled and minified CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css"> <!-- Compiled and minified JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/js/materialize.min.js"></script> <form> <div class="row"> <div class="input-field col s6"> <input checked required class="validate" type="radio" name="mode" value="M" id="m"> <label for="m">M</label> </div> <div class="input-field col s6"> <input required class="validate" type="radio" name="mode" value="F" id="f"> <label for="f">F</label> </div> <div class="col s12" style="margin-top:20px"> <button type="submit" class="waves-effect waves-green btn"> Submit </button> </div> </div> </form>

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

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