ヘッダー
C# Sample Programs
 

Validate if the input value is within the range

04/18/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 2 ≦ the input value ≦ 99

In this example, the boundary values (2 and 99) are enterable.

RangeValidation.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace WorkStandard.Pages.validation
{
    public class RangeValidationModel : PageModel
    {
        [BindProperty]
        [DisplayName("Count")]
        [Range(2, 99, ErrorMessage = "The {0} must be within the range {1} to {2}")]
        public int? Count { get; set; }

        public void OnPost()
        {
            if (!ModelState.IsValid)
            {
                return;
            }

            System.Diagnostics.Debug.WriteLine($"No errors {Count}");
        }
    }
}

Where Debug.WriteLine outputs to

 

RangeValidation.cshtml

@page
@model WorkStandard.Pages.validation.RangeValidationModel

<form method="post">
    <div class="mb-3">
        <label asp-for="Count"></label>
        <input asp-for="Count" class="form-control" />
        <span asp-validation-for="Count" class="text-danger"></span>
    </div>
    <div>
        <input type="submit" class="btn btn-primary" />
    </div>
</form>

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

 

 

Validate 2 ≦ the input value < 60

This example works with .NET 8 (2023) and later.

In this example, you can input 2 and 59.9, but 60 will be rejected.

RangeExclusiveValidation.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace WorkStandard.Pages.validation
{
    public class RangeExclusiveValidationModel : PageModel
    {
        [BindProperty]
        [DisplayName("Weight")]
        [Range(2D, 60D, MaximumIsExclusive = true, ErrorMessage = "The {0} must be greater than or equal to {1} and less than {2}")]
        public double? Weight { get; set; }

        public void OnPost()
        {
            if (!ModelState.IsValid)
            {
                return;
            }

            System.Diagnostics.Debug.WriteLine($"No errors {Weight}");
        }
    }
}

Where Debug.WriteLine outputs to

Note: If you need exclusive minimum value, you must set MinimumIsExclusive = true.

 

RangeExclusiveValidation.cshtml

@page
@model WorkStandard.Pages.validation.RangeExclusiveValidationModel

<form method="post">
    <div class="mb-3">
        <label asp-for="Weight"></label>
        <input asp-for="Weight" class="form-control" />
        <span asp-validation-for="Weight" class="text-danger"></span>
    </div>
    <div>
        <input type="submit" class="btn btn-primary" />
    </div>
</form>

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

 

 

Validate 01/01/1900 ≦ the input value < 12/31/2099

RangeDateValidation.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace WorkStandard.Pages.validation
{
    public class RangeDateValidationModel : PageModel
    {
        [BindProperty]
        [DisplayName("Date of birth")]
        [DataType(DataType.Date)]
        [Range(typeof(DateTime), "1900/1/1", "2099/12/31", ErrorMessage = "The {0} must be within the range {1:MM/dd/yyyy} to {2:MM/dd/yyyy}")]
        public DateTime? BirthDate { get; set; } = DateTime.Now;

        public void OnPost()
        {
            if (!ModelState.IsValid)
            {
                return;
            }

            System.Diagnostics.Debug.WriteLine($"No errors {BirthDate}");
        }
    }
}

Where Debug.WriteLine outputs to

 

RangeDateValidation.cshtml

@page
@model WorkStandard.Pages.validation.RangeDateValidationModel

<form method="post">
    <div class="mb-3">
        <label asp-for="BirthDate"></label>
        <input asp-for="BirthDate" class="form-control" />
        <span asp-validation-for="BirthDate" class="text-danger"></span>
    </div>
    <div>
        <input type="submit" class="btn btn-primary" />
    </div>
</form>

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

 


日本語版