ASP.NET Program to use custom validator control

Use custom validator control with following requirements:

  • Passwords are between 6 and 14 characters in length.
  • Must contain at least one uppercase letter, one lowercase letter, and one numeric character.
Answer:


Introduction:
If the existing asp.net control does not fulfill our need, then we can define custom validator based on our need. In the custom validator control, we can use our own logic to validate the different entries.

In this application, we use custom validator for password check. The password must be 6 to 14 characters in length with at least one uppercase, one lowercase and one numeric character.

password-val.aspx

<%@ Page Language="VB" %>
<!DOCTYPE html>
<script runat="server">
    Private Sub Page_Load(sender As Object, e As EventArgs)
        If Request.RequestType = "POST" Then
            Dim password As String = Request.Form("pwd")
            If password.Length >= 6 OrElse password.Length <= 14 Then
                If System.Text.RegularExpressions.Regex.IsMatch(password, "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,14}$") Then
                    Response.Write("Valid Password")
                Else
                    Response.Write("Password must contain: 6 - 14 characters atleast 1 UpperCase Alphabet, 1 LowerCase Alphabet and 1 Number")
                End If
            End If
        End If
    End Sub
</script>
<head>
      <title>Employee Registration Page</title>
</head>
<body>
</body>
<form id="form1" method="post" runat="server">
      Password:<asp:textbox ID="pwd" runat="server" TextMode="Password"></asp:textbox>
     <asp:Button ID="submit" runat="server" type="Submit" Text="Submit" /></br>
     <asp:RequiredFieldValidator ID="validpwd" runat="server" ControlToValidate="pwd" ErrorMessage="Required!" ForeColor="Red"></asp:RequiredFieldValidator>
  </form>
</html>


Output:

1.
custom validator control

2. User does not enter any thing

custom validator control

3. The password pattern is not matched

custom validator control

4. Password is matched in given format

custom validator control