11

I have got a task to set red color border for text box when the validation fails in .net mvc 4.

BExtensionMethods.cs

    public static string GetTextBoxColor(this ModelStateDictionary ModelState)
    {
        string textBoxColor = string.Empty;
        int count = 1;
        var errorKeys = (from item in ModelState
                         where item.Value.Errors.Any()
                         select item.Key).ToList();
        foreach (var item in errorKeys)
        {
            textBoxColor += string.Format("{0}.{1}</br>", count, item);
            count++;
        }
        return textBoxColor;
    }

Here the json object contains the values.How can I filter it?

Nithin Viswanathan
  • 3,245
  • 7
  • 39
  • 84

6 Answers6

20
if ($('#TextBoxID').val() == '') {
    $('#TextBoxID').css('border-color', 'red');
}
else {
    $('#TextBoxID').css('border-color', '');
}
Mohan Kumar
  • 647
  • 7
  • 8
17

You need to make a css class like that:

.errorClass { border:  1px solid red; }

And add it to your textbox whith jQuery:

$("#myTextBox").addClass('errorClass');
PanzerKadaver
  • 363
  • 3
  • 12
1

Just copy the below code in your project u will get to know, i'm using purely bootstrap and jquery here.

<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.min.js"></script>

<style type="text/css">
/**
 * Override feedback icon position
 * See http://formvalidation.io/examples/adjusting-feedback-icon-position/
 */
#eventForm .dateContainer .form-control-feedback {
    top: 0;
    right: -15px;
}
</style>

<form id="eventForm" method="post" class="form-horizontal">
    <div class="form-group">
        <label class="col-xs-3 control-label">Event</label>
        <div class="col-xs-5">
            <input type="text" class="form-control" name="name" />
        </div>
    </div>

    <div class="form-group">
        <label class="col-xs-3 control-label">Start date</label>
        <div class="col-xs-5 dateContainer">
            <div class="input-group input-append date" id="startDatePicker">
                <input type="text" class="form-control" name="startDate" />
                <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-xs-3 control-label">End date</label>
        <div class="col-xs-5 dateContainer">
            <div class="input-group input-append date" id="endDatePicker">
                <input type="text" class="form-control" name="endDate" />
                <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="col-xs-5 col-xs-offset-3">
            <button type="submit" class="btn btn-default">Validate</button>
        </div>
    </div>
</form>

<script>
$(document).ready(function() {
    $('#startDatePicker')
        .datepicker({
            format: 'mm/dd/yyyy'
        })
        .on('changeDate', function(e) {
            // Revalidate the start date field
            $('#eventForm').formValidation('revalidateField', 'startDate');
        });

    $('#endDatePicker')
        .datepicker({
            format: 'mm/dd/yyyy'
        })
        .on('changeDate', function(e) {
            $('#eventForm').formValidation('revalidateField', 'endDate');
        });

    $('#eventForm')
        .formValidation({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                name: {
                    validators: {
                        notEmpty: {
                            message: 'The name is required'
                        }
                    }
                },
                startDate: {
                    validators: {
                        notEmpty: {
                            message: 'The start date is required'
                        },
                        date: {
                            format: 'MM/DD/YYYY',
                            max: 'endDate',
                            message: 'The start date is not a valid'
                        }
                    }
                },
                endDate: {
                    validators: {
                        notEmpty: {
                            message: 'The end date is required'
                        },
                        date: {
                            format: 'MM/DD/YYYY',
                            min: 'startDate',
                            message: 'The end date is not a valid'
                        }
                    }
                }
            }
        })
        .on('success.field.fv', function(e, data) {
            if (data.field === 'startDate' && !data.fv.isValidField('endDate')) {
                // We need to revalidate the end date
                data.fv.revalidateField('endDate');
            }

            if (data.field === 'endDate' && !data.fv.isValidField('startDate')) {
                // We need to revalidate the start date
                data.fv.revalidateField('startDate');
            }
        });
});
</script>
PK-1825
  • 1,431
  • 19
  • 39
0

If you have only one textbox and id is known to you , you can use the @PanzerKadaver solution. Otherwise i would suggest to return in the json it self the ids of the textboxes which you want to make the red . Then loop through it and add the error class on the client side.

Devesh
  • 4,500
  • 1
  • 17
  • 28
  • My all data comes under this variable- objRenewableEnergy.How can I filter the values from objRenewableEnergy – Nithin Viswanathan Apr 24 '13 at 08:12
  • use can use the json notation result.renewableEnergy and loop through it. In case you want to see the exact JSON , using the developer tool of the IE using F12 and see the structure of the JSON coming with all the properties. You can loop usin $.each of the jquery – Devesh Apr 24 '13 at 08:15
  • for (var n = 0; n < objRenewableEnergy;n++ ) { } is it correct? – Nithin Viswanathan Apr 24 '13 at 08:36
0

result.renewableEnergy will give you the value you need. Any other properties of the objRenewableEnergy can be accessed by result.renewableEnergy.property

Ashwin Singh
  • 7,197
  • 4
  • 36
  • 55
-1
public static List<string> GetTextBoxColor(this ModelStateDictionary ModelState)
    {
        string textBoxColor = string.Empty;
        int count = 1;
        List<string> list = new List<string>();
        var errorKeys = (from item in ModelState
                         where item.Value.Errors.Any()
                         select item.Key.Substring(item.Key.LastIndexOf('.')).Trim('.')).ToList();
        foreach (var item in errorKeys)
        {
            textBoxColor += string.Format("{0}.{1}</br>", count, item);
            list.Add(item);
            count++;
        }
        return list;

    }
Nithin Viswanathan
  • 3,245
  • 7
  • 39
  • 84