0

I used to do this in a desktop app like this:

    private void txtSerials_TextChanged(object sender, EventArgs e)
    {
        //Count serials            
        SerialNumbers = Regex.Split(txtSerials.Text.Trim(), "\r\n");
        lblSerCount.Text = SerialNumbers.Length.ToString();
    }

How can I achieve the same in jquery?

Somebody
  • 2,667
  • 14
  • 60
  • 100

2 Answers2

1

This function translates the functionality from your C# to JavaScript and jQuery:

$("#txtSerials").change(function (e) {
    var serialNumbers = this.value.trim().split(/\n/);
    $("#lblSerCount").text(serialNumbers.length);
});

The trim() method is not supported in IE8 or earlier. You can fix that by including this JavaScript polyfill from MDN:

if (!String.prototype.trim) {
    String.prototype.trim = function () {
        return this.replace(/^\s+|\s+$/g, '');
    };
}
gilly3
  • 87,962
  • 25
  • 144
  • 176
0
var checkthis = $("#IDofyourtextboxhere").val();
var newlines = checkthis.match(/\n/g);
var numberoflines = newlines.length;
shenn
  • 859
  • 4
  • 17
  • 47