None
None

Reputation: 9267

How to change colour of textbox while typing in textbox?

I'm using Validating form input fields while typing using jQuery and my script is looks like this:

var firstname = /^[A-Za-z]{3,15}$/;
var day = $("#day_birthdate");
jQuery('input#firstname').bind('input propertychange', function () {
    if (firstname.test(jQuery(this).val())) {
        jQuery(this).css({
            'background': '#C2D699'
        });

    } else {
        jQuery(this).css({
            'background': '#FFC2C2'
        });

    }
});


jQuery('input#day_birthdate').bind('input propertychange', function () {
    if (day > 1) {
        jQuery(this).css({
            'background': 'green'
        });

    } else {
        jQuery(this).css({
            'background': 'red'
        });

    }
});

I have textbox:

 @Html.TextBoxFor(m => m.Register.FirstName, new { id = "firstname", @class = "form-control", @maxlength = "16" })
 @Html.TextBoxFor(m => m.Register.Day, new { id = "day_birthdate", @class = "form-control", mask = "99", @placeholder = "dd" })

Main idea is to check in javascript if day is between 1 and 31 to get green colour of background while im typing it, if its not to get red colour?

Upvotes: 1

Views: 413

Answers (1)

Tushar
Tushar

Reputation: 87233

You need to get the value of #day_birthdate and then parse the value of day to integer:

var day = parseInt($("#day_birthdate").val(), 10);
//                                    ^^^^^^

Upvotes: 1

Related Questions