babsevensix babsevensix - 1 year ago 152
C# Question

Show asterisk with NotEmpty using Fluentvalidation

I want to show on Asp.Net MVC 4 web page a

with asterisk when property have

Answer Source

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


 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.

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download