How to set the textbox border color red when validation fails

Nithin Viswanathan picture Nithin Viswanathan · Apr 24, 2013 · Viewed 92.3k times · Source

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?

Answer

Mohan Kumar picture Mohan Kumar · Sep 4, 2014
if ($('#TextBoxID').val() == '') {
    $('#TextBoxID').css('border-color', 'red');
}
else {
    $('#TextBoxID').css('border-color', '');
}