WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="userreg.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>User Registration</title>
<style>
.error { color: red; }
.form-field { margin-bottom: 10px; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Registration Form</h2>
<div class="form-field">
Name:
<asp:TextBox ID="txtName" runat="server" AutoPostBack="true" OnTextChanged="txtName_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server"
ControlToValidate="txtName" ErrorMessage="Name is required" CssClass="error"></asp:RequiredFieldValidator>
</div>
<div class="form-field">
Age:
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAge" runat="server"
ControlToValidate="txtAge" ErrorMessage="Age is required" CssClass="error"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="rvAge" runat="server"
ControlToValidate="txtAge" MinimumValue="20" MaximumValue="30" Type="Integer"
ErrorMessage="Age must be between 20 and 30" CssClass="error"></asp:RangeValidator>
</div>
<div class="form-field">
Email ID:
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEmail" runat="server"
ControlToValidate="txtEmail" ErrorMessage="Email is required" CssClass="error"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revEmail" runat="server"
ControlToValidate="txtEmail"
ErrorMessage="Invalid Email"
ValidationExpression="^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
CssClass="error">
</asp:RegularExpressionValidator>
</div>
<div class="form-field">
User ID:
<asp:TextBox ID="txtUserID" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvUserID" runat="server"
ControlToValidate="txtUserID" ErrorMessage="UserID is required" CssClass="error"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revUserID" runat="server"
ControlToValidate="txtUserID"
ErrorMessage="UserID must contain a capital letter and a digit, 7-20 chars"
ValidationExpression="^(?=.*[A-Z])(?=.*\d).{7,20}$"
CssClass="error">
</asp:RegularExpressionValidator>
</div>
<div class="form-field">
Password:
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server"
ControlToValidate="txtPassword" ErrorMessage="Password is required" CssClass="error"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revPassword" runat="server"
ControlToValidate="txtPassword"
ErrorMessage="Password must be 7-20 chars"
ValidationExpression="^.{7,20}$"
CssClass="error">
</asp:RegularExpressionValidator>
</div>
<div class="form-field">
<asp:Button ID="btnSubmit" runat="server" Text="Register" OnClick="btnSubmit_Click" />
</div>
<asp:Label ID="lblMessage" runat="server" ForeColor="Green"></asp:Label>
</div>
</form>
</body>
</html>
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace userreg
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
}
protected void txtName_TextChanged(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblMessage.Text = "Registration Successful!";
// Optional: Save data to database here
}
}
}
}
Steps:
1. Choose ASP.NET Web Application (.NET Framework)
2. Choose empty and Check the Web Forms
3. Right click on solution explorer and add a new item web forms and edit the codes and paste the above 2 codes
4. make sure to right click on webform1.aspx and click set as start page
5. click run
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter