Categories: JavaScript

Login Form Validation with JavaScript

Published by

Hi,

Lets come back to our ASP.NET series. This time we are sharing a simple tip for validating Login form using JavaScript.

Login Form Validation

Lets have look at our ASP.NET login form.

<table align="center" border="0">
    <tr>
        <td align="right" class="TxtLabels">Username:
        </td>
        <td class="SpacerCell">&nbsp;
        </td>
        <td align="left">
            <asp:TextBox ID="TextBox1" runat="server" ToolTip="Enter your Username, Email ID or Mobile Number Here."
                CssClass="TextBox" TabIndex="1"> </asp:TextBox>
        </td>
        <td>
            <asp:LinkButton ID="LinkButton1" runat="server" ToolTip="Click here in case you forgot your Username!"
                CssClass="TxtLabels" Text="Forgot Username?"></asp:LinkButton>
        </td>
    </tr>
    <tr>
        <td align="right" class="TxtLabels">Password:
        </td>
        <td class="SpacerCell">&nbsp;
        </td>
        <td align="left">
            <asp:TextBox ID="TextBox2" runat="server" ToolTip="Your Password or Secret Code."
                TextMode="Password" CssClass="TextBox" TabIndex="2"></asp:TextBox>
        </td>
        <td>
            <asp:LinkButton ID="LinkButton2" runat="server" ToolTip="Click here in case you forgot your Password!"
                CssClass="TxtLabels" Text="Forgot Password?" OnClick="LnkPassword_Click"></asp:LinkButton>
        </td>
    </tr>
    <tr>
        <td class="SpacerCell">&nbsp;
        </td>
        <td class="SpacerCell">&nbsp;
        </td>
        <td align="center">
            <asp:Button ID="Button1" runat="server" Text="Enter" ToolTip="Login/Signin to your very own Network."
                CssClass="BtnSignIn" OnClientClick="return ValidateForm();" OnClick="BtnLogIn_Click" TabIndex="3" />
            &nbsp;&nbsp;&nbsp;&nbsp;
                                        <input type="reset" title="Clear Form and Re-Enter your Credentials!" value="Re-Enter"
                                            class="BtnSignIn" />
        </td>
        <td>
            <asp:Label ID="Label1" runat="server" ToolTip="Sorry, This is the Actual Reason for Failure!"
                CssClass="TxtLabels"></asp:Label>
        </td>
    </tr>
</table>

This design will look similar like below screen.

ShikshaNet-LoginForm

Now lets validate this form using JavaScript so, user can be assisted more easily and gets exact error message quickly before sending request to Server.

JavaScript code

    <script language="javascript" type="text/javascript">
        function ValidateForm() {
            var User = document.getElementById("ContentPlaceHolder1_TxtUserName").value;
            var Pass = document.getElementById("ContentPlaceHolder1_TxtPassword").value;

            if (User == "" && Pass == "") {
                alert("Username and Password are Necessory!, Please enter and try again.");

                document.getElementById("ContentPlaceHolder1_TxtUserName").focus();
                return false;
            }
            else if (Pass == "") {
                alert("Enter Password for : n" + User.toString());

                document.getElementById("ContentPlaceHolder1_TxtUserName").focus();
                return false;
            }
            else if (User.length < 4) {
                alert("Invalid Length. Username should be at least 4 character long.");

                document.getElementById("ContentPlaceHolder1_TxtUserName").focus();
                return false;
            }
            else if (Pass.length < 4 || Pass.length >= 18) {
                alert("Invalid Length!, Password must be at least 4 character and at most 18 in Length.");
                document.getElementById("ContentPlaceHolder1_TxtPassword").focus();
                return false;
            }
            else if (User.length == 0) {
                alert("Enter Username! Can not be blank!");

                document.getElementById("ContentPlaceHolder1_TxtUserName").focus();
                return false;
            }

            return true;
        }
    </script>

Now, lets have look at above JavaScript code.

Related Post

[box type=”info” align=”alignleft” class=”” width=””]1. If user name and password are blank, there will be a error message.

2. If password is not entered, a alert will thrown with Username.

3. If user name is less than 4 character, there will be another error message.

4. If password is outside 4 character and 18 character, you will see an alert.[/box]

Lets have a look at output below.

Share your views via comments and do not forget to share among your friends.

Advertisement
Share

View Comments

  • Have you ever considered about adding a little bit more than just your articles? I mean, what you say is important and everything. However imagine if you added some great images or videos to give your posts more, "pop"! Your content is excellent but with images and clips, this blog could certainly be one of the most beneficial in its field. Excellent blog!

Published by

Recent Posts

Understanding Functions in C: Best Beginner’s Guide

In this blog, we'll explore the concept of Functions in C, their syntax, type of… Read More

Last modified 4 days ago

What is Queue in Data Structure and Algorithm, Concept of Queue in DSA Explained

First of all we have to understand what is queue? Queue in Data Structure and… Read More

Last modified 2 weeks ago

How To Change Bootstrap 5 Theme between Light and Dark Mode in any webpage using JavaScript & CSS?

In this article, we will learn how to add Dark and Light Mode switching functionality… Read More

Last modified 3 weeks ago