JavaScript Regular expression validation

JavaScript Validate the value by regular expression

1

Username validation

starting with [a-z] and [a-z0-9_] letters only. min-max 5-20
JavaScript
function validateUsername(username) {
    var regex = /^[a-z][a-z0-9_]{4,20}$/gi;
    return regex.test(username);
}
HTML
<label for="username">username</label>
<div>
    <input id="username" class="input-text" type="text">
    <span id="username-valid-label" style="display: block;color: red"></span>
</div>

<script>
document.getElementById("username").addEventListener("change", function () {
    var username = this.value;
    var result = validateUsername(username);
    var label = document.getElementById("username-valid-label");
    if (result) {
        label.innerText = '';
    } else {
        label.innerText = 'Invalid string. only [a-z0-9_] min-max 5-20';
    }
});
</script>
2

Email validation

3

Date validation

4

Strong password validation

5

URL link validation

6

Mobile number validation