I have some requirement as below:
- Minimum character is 3 and maximum is 8.
- Only accept Capital Letter and Underscore.
- The 1st three (3) characters must be Uppercase.
Example acceptable input: “SAL”, “SAL_”, “SAL_G”
The problem now:
- How to control user must enter compulsory fill 1st three character with Uppercase?
- The underscore is not appear when press at the keyboard
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;
}
});