input validation javascript with login form code example

Example 1: javascript login validation

<html>
<body>
<form id="form1" runat="server">
    <div class="container">
        <div class="main">
            <h2>
                Javascript Login Form Validation</h2>
            <form id="form_id" method="post" name="myform">
            <label>
                User Name :</label>
            <input type="text" name="username" id="username" />
            <br><br>
            <label>
                Password :</label>
            <input type="password" name="password" id="password" />
            <input type="button" value="Login" id="submit" onclick="validate();" />
            </form>
        </div>
    </div>
    </form>
</html>
</body>

Example 2: how to set validation for email in javascript

<script language="javascript">

function checkEmail() {

    var email = document.getElementById('txtEmail');
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (!filter.test(email.value)) {
    alert('Please provide a valid email address');
    email.focus;
    return false;
 }
}</script>