ASP.NET - Experiment No 4 Email Validation

Write a code snippet in C# to validate user input in an ASP.NET web form. Include validation for required fields, email format, and password strength.

Chashvalidation.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidationDemo.aspx.cs" 
Inherits="Chashvalidation.ValidationDemo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>Validation Demo</title>
 <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>

<body>
 <form id="form1" runat="server">
 <div>
 <h2>User Registration</h2>
 <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
 <asp:Label ID="lblUsername" runat="server" Text="Username:"></asp:Label>
 <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
 <asp:RequiredFieldValidator ID="rfvUsername" runat="server" 
ControlToValidate="txtUsername"
 ErrorMessage="Username is required." Display="Dynamic" 
ForeColor="Red"></asp:RequiredFieldValidator>
 <br />
 <asp:Label ID="lblEmail" runat="server" Text="Email:"></asp:Label>
 <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
 <asp:RequiredFieldValidator ID="rfvEmail" runat="server" 
ControlToValidate="txtEmail"
 ErrorMessage="Email is required." Display="Dynamic" 
ForeColor="Red"></asp:RequiredFieldValidator>
 <asp:RegularExpressionValidator ID="revEmail" runat="server" 
ControlToValidate="txtEmail"
 ErrorMessage="Invalid email format." Display="Dynamic" ForeColor="Red"
 ValidationExpression="\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|az]{2,}\b"></asp:RegularExpressionValidator>
 <br />
 <asp:Label ID="lblPassword" runat="server" Text="Password:"></asp:Label>
 <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
 <asp:RequiredFieldValidator ID="rfvPassword" runat="server" 
ControlToValidate="txtPassword"
 ErrorMessage="Password is required." Display="Dynamic" 
ForeColor="Red"></asp:RequiredFieldValidator>
 <asp:RegularExpressionValidator ID="revPassword" runat="server" 
ControlToValidate="txtPassword"
 ErrorMessage="Password must be at least 8 characters long and contain at least 
one uppercase letter, one lowercase letter, and one digit."
 Display="Dynamic" ForeColor="Red"
 ValidationExpression="^(?=.[a-z])(?=.[AZ])(?=.*\d).{8,}$"></asp:RegularExpressionValidator>
 <br />
 <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
OnClick="btnSubmit_Click" />
 <asp:Label ID="lblValidationResult" runat="server" Text="" 
ForeColor="Green"></asp:Label>
 </div>
 </form>
</body>
</html>

Chashvalidation.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Chashvalidation
{
 public partial class ValidationDemo : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {
 }
 protected void btnSubmit_Click(object sender, EventArgs e)
 {
 if (Page.IsValid)
 {
 // Your code to process the form data
 lblValidationResult.Text = "Form submitted successfully!";
 }
 else
 {
 lblValidationResult.Text = "Please fix the validation errors.";
 }
 }
 }
}

Webconfig file

<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>

https://github.com/PratikshaGhanawat