C# Sample Programs |
Site Top > C# Sample Programs > C# Sample Programs > ASP.NET Core > Razor Pages
String Length Validation
01/05/2024
This article deals with ASP.NET Core Razor Pages.
All sample programs in this article are based on the Visual Studio Project template "ASP.NET Core Web App" and you must add the Razor pages in th Pages folder.
Validate input is within 8 characters
StringLengthValidation.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace WorkStandard.Pages.validation
{
public class StringLengthValidationModel : PageModel
{
[BindProperty]
[DisplayName("Student ID")]
[StringLength(8, ErrorMessage = "{0} must be within {1} characters")]
public string? studentId { get; set; }
public void OnPost()
{
//Server side validation check
if (!ModelState.IsValid)
{
return;
}
System.Diagnostics.Debug.WriteLine($"No errors. {studentId}");
}
}
}
→ Where Debug.WriteLine outputs to
StringLengthValidation.cshtml
@page
@model WorkStandard.Pages.validation.StringLengthValidationModel
<form method="post">
<div class="mb-3">
<label asp-for="studentId"></label>
<input asp-for="studentId" class="form-control" />
<span asp-validation-for="studentId" class="text-danger"></span>
</div>
<div>
<input type="submit" class="btn btn-primary" />
</div>
</form>
@section Scripts {
@{
//Enable client side validation.
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
Note: As in this example, you must check validation result at server side. Malicious users can easily bypass client side validation to attack the server.
The CSS classes...
- mb-3 Set little margin at bottom.→ Spacing
- form-control Make the input a Bootstrap look.→ Forms
- text-danger The string become red to indicate danger.→ Theme colors
- btn btn-primary Give the button a Bootstrap look to indicate that it's a key feature.→ Buttons
Note: In this sample, The meaning of the number of Characters is same as result of String.Length. If you are not familiar with this see String Length .
Note: To test input over 8 characters, add maxlength="100" explicitly to the input element.
Validate input is between 3 and 8 characters
StringMinLengthValidation.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace WorkStandard.Pages.validation
{
public class StringMinLengthValidationModel : PageModel
{
[BindProperty]
[DisplayName("Student ID")]
[Required(ErrorMessage = "{0} is required.")]
[StringLength(8, MinimumLength = 3, ErrorMessage = "{0} must be between {2} and {1} characters.")]
public string? studentId { get; set; }
public void OnPost()
{
//Server side validation check
if (!ModelState.IsValid)
{
return;
}
System.Diagnostics.Debug.WriteLine($"No errors. {studentId}");
}
}
}
→ Where Debug.WriteLine outputs to
StringMinLengthValidation.cshtml
@page
@model WorkStandard.Pages.validation.StringMinLengthValidationModel
<form method="post">
<div class="mb-3">
<label asp-for="studentId"></label>
<input asp-for="studentId" class="form-control" />
<span asp-validation-for="studentId" class="text-danger"></span>
</div>
<div>
<input type="submit" class="btn btn-primary" />
</div>
</form>
@section Scripts {
@{
//Enable client side validation.
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
Note: As in this example, you must check validation result at server side. Malicious users can easily bypass client side validation to attack the server.
The CSS classes...
- mb-3 Set little margin at bottom.→ Spacing
- form-control Make the input a Bootstrap look.→ Forms
- text-danger The string become red to indicate danger.→ Theme colors
- btn btn-primary Give the button a Bootstrap look to indicate that it's a key feature.→ Buttons
Note: In this sample, The meaning of the number of Characters is same as result of String.Length. If you are not familiar with this see String Length .
Note: To test input over 8 characters, add maxlength="100" explicitly to the input element.