使用Fluentvalidation在NotEmpty上显示星号 [英] Show asterisk with NotEmpty using Fluentvalidation

查看:61
本文介绍了使用Fluentvalidation在NotEmpty上显示星号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当属性具有NotEmpty规则时,我想在Asp.Net MVC 4网页上显示带星号的label.

I want to show on Asp.Net MVC 4 web page a label with asterisk when property have NotEmpty rule.

推荐答案

只需将此函数添加到您的常用脚本函数中并在加载时运行

Just add this function to your common script functions and run on load

$(document).ready(function(){
     setRequired();  
 });

 function setRequired() {
    var $form = $('form');
    $('form').find("[data-val-required]").each(function (index) {
        var $input = $(this); 
        var requiredAsterisk = "<span class=\"required\">*</span>";
        var id = $input.attr('id');
        var $label = $form.find("label[for='" + id + "']");
        if ($label.length > 0) {
            var html = $label.html() + "";
            if (html.indexOf(requiredAsterisk) <= 0) $label.html(html + requiredAsterisk);
        }
    });
};

这是做什么的,它将星号添加到必填字段的标签中.您可以轻松地对其进行更改,以将其添加到输入字段旁边.

What this does it , it adds asterisk to labels of required fields. You can easily change it to add it right next to input fields.

要注意的另一件事是我使用css class = required,因此请记住为此CSS类添加color:red.

Another thing to note is I use css class = required, so remember to add the color:red for this css class.

这篇关于使用Fluentvalidation在NotEmpty上显示星号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆