To centralize the pattern and title attributes for your textboxes, you can define them in a single location and reference them in your ASPX pages. Here’s how you can do it:
Step 1: Define the Pattern and Title in Web.config
Add the pattern and title as app settings in your Web.config
file.
<configuration>
<appSettings>
<add key="TextBoxPattern" value="[a-zA-Z\s\.]*" />
<add key="TextBoxTitle" value="Valid characters: Alphabets/Space & . (Dot)" />
</appSettings>
</configuration>
Step 2: Create a Helper Method in Code-Behind
Create a helper method in your base page or a utility class to retrieve these settings.
public static class Utility
{
public static string GetTextBoxPattern()
{
return System.Configuration.ConfigurationManager.AppSettings["TextBoxPattern"];
}
public static string GetTextBoxTitle()
{
return System.Configuration.ConfigurationManager.AppSettings["TextBoxTitle"];
}
}
Step 3: Use the Helper Method in Your ASPX Pages
Use the helper method to set the pattern and title attributes in your ASPX pages.
<%@ Import Namespace="YourProjectNamespace" %>
<div class="col-lg-6 col-md-6 col-sm-12">
<div class="form-group">
<label>Department Name</label> <span style="color: red">*</span>
<asp:TextBox ID="txtDepartmentName" MinLength="2" MaxLength="50" class="form-control" placeholder="Department Name" required="true"
pattern="<%= Utility.GetTextBoxPattern() %>" title="<%= Utility.GetTextBoxTitle() %>" runat="server"></asp:TextBox>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label>Short Name</label>
<asp:TextBox ID="txtShortName" MinLength="2" MaxLength="15" class="form-control" placeholder="Short Name" required="true"
pattern="<%= Utility.GetTextBoxPattern() %>" title="<%= Utility.GetTextBoxTitle() %>" runat="server"></asp:TextBox>
</div>
</div>
Step 4: Implement SweetAlert for Validation Messages
To display validation messages using SweetAlert, you can use JavaScript to check the validity of the textboxes and show the alert.
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
function validateForm() {
var isValid = true;
var textboxes = document.querySelectorAll('input[type="text"]');
textboxes.forEach(function (textbox) {
if (!textbox.checkValidity()) {
isValid = false;
Swal.fire({
icon: 'error',
title: 'Invalid Input',
text: textbox.title
});
return false;
}
});
return isValid;
}
document.getElementById('searchForm').onsubmit = function () {
return validateForm();
};
</script>
This setup ensures that any changes to the pattern or title are reflected across all your ASPX pages, and SweetAlert is used to display validation messages.
Thanks