Hi Simflex,
Sorry for the late answer. I tried recreating this problem but I found that your code largely works fine (aside from Sub txtdob_TextChanged missing End Sub) and TryParseExact can already handle dates properly. In your case maybe you want to make sure the date doesn't exceed a certain threshold (such as birthday couldn't be later than current date)? then I would just add a check like:
dateValue > DateTime.Today
Here's my recreation:
.aspx:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="PasswordFormatValidator._Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Password Format Validator</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server" class="container mt-5">
<div class="mb-3">
<label for="txtdob" class="form-label">Enter password (e.g., JD01011900):</label>
<asp:TextBox ID="txtdob" runat="server" CssClass="form-control" MaxLength="10"
Placeholder="First letter of first + last name + MMDDYYYY"
AutoPostBack="true" OnTextChanged="txtdob_TextChanged" />
</div>
<asp:Label ID="emplabel" runat="server" CssClass="form-text"></asp:Label>
</form>
</body>
</html>
aspx.vb:
Imports System.Text.RegularExpressions
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub txtdob_TextChanged(sender As Object, e As EventArgs) Handles txtdob.TextChanged
Dim input As String = txtdob.Text.Trim()
If String.IsNullOrEmpty(input) Then
emplabel.ForeColor = Drawing.Color.Red
emplabel.Text = "Empid is required!"
txtdob.Focus()
Return
End If
Dim isValid As Boolean = Validator.ValidateString(input)
If Not isValid Then
emplabel.ForeColor = Drawing.Color.Red
emplabel.Text = "Invalid format. Example John Doe, JD02262025"
txtdob.Focus()
Else
emplabel.ForeColor = Drawing.Color.Green
emplabel.Text = "Valid format!"
End If
End Sub
Public Class Validator
Public Shared Function ValidateString(ByVal input As String) As Boolean
' Check format: 2 letters followed by 8 digits
If input.Length <> 10 Then Return False
Dim pattern As String = "^[A-Za-z]{2}\d{8}$"
If Not Regex.IsMatch(input, pattern) Then Return False
' Validate date portion (MMDDYYYY)
Dim datePart As String = input.Substring(2, 8)
Dim dateValue As DateTime
If Not DateTime.TryParseExact(datePart, "MMddyyyy", Nothing, Globalization.DateTimeStyles.None, dateValue) Then
Return False
End If
' Date must not be in the future
If dateValue > DateTime.Today Then
Return False
End If
Return True
End Function
End Class
End Class
Hope I interpreted this correctly. Please tell me if you have any issues.