How to solve JS Regex not working during keypress

I have some requirement as below:

  1. Minimum character is 3 and maximum is 8.
  2. Only accept Capital Letter and Underscore.
  3. The 1st three (3) characters must be Uppercase.

Example acceptable input: “SAL”, “SAL_”, “SAL_G”

The problem now:

  1. How to control user must enter compulsory fill 1st three character with Uppercase?
  2. The underscore is not appear when press at the keyboard

enter image description here

HTML

<input class="form-control" type="text" id="myInput" minlength="3" maxlength="8">

JS

$('#myInput').on("keypress", function (e) {
    var regex = new RegExp("^[A-Z]+(?:_[A-Z]+)*$")
    var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
    if (regex.test(str)) {
        return true;
    } else {
        return false;
    }
});