如何在Razor .NET Core中进行日期验证

发布于 2025-01-29 22:42:02 字数 255 浏览 3 评论 0原文

需要标准验证来开始和结束日期。应包括:

  1. 结束日期>开始日期。
  2. 结束日期今天日期。
  3. 开始日期< 结束日期

到目前为止的 在下面尝试了下面的方法,但是这些方法并不能完全起作用: ASP MVC DateTime范围数据从现在到20年?

请建议。

Need standard validations for Start and End Dates. Should include:

  1. End Date > Start Date.
  2. End Date < Today Date.
  3. Start Date < End Date

So far tried below approaches, but these do not work completely:
ASP MVC Datetime range data annotation from now to 20 years?

Please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2025-02-05 22:42:02

您可以使用Fluent验证来实现这一目标。我在这里写一个简单的演示,希望它能为您提供帮助。

demo

 public class Time
    {
        public DateTime Start { get; set; }
        public DateTime End  { get; set; }
    }

dateTimeValidator

public class DateTimeValidator : AbstractValidator<Time>
    {
      
        public DateTimeValidator()
        {
            DateTime now = DateTime.Now;

            RuleFor(x => x.Start)
                .LessThan(x => x.End)
                .WithMessage("Start must < End")
                .NotEmpty(); 

            RuleFor(x => x.End)
                .GreaterThan(x => x.Start)
                .WithMessage("End must > Start")
                .NotEmpty();
        }
    }

page

@model Time

<form asp-action="Time" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Start" class="control-label" ></label>
                <input asp-for="Start" class="form-control" type="date" max='' id="startDate"/>
                <span asp-validation-for="Start" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="End" class="control-label"></label>
                <input asp-for="End" class="form-control" type="date" max='' id="endDate"/>
                <span asp-validation-for="End" class="text-danger"></span>
            </div>
            <button type="submit">submit</button>
</form>

@section  Scripts{
    <script>
        //I use js to allow users to select only the latest date up to yesterday
        function addZero(n) {
            return parseInt(n) >= 10 ? n.toString() : '0' + n;
        }
        let dateNow = new Date(),
            yearNow = dateNow.getFullYear(),
            monthNow = dateNow.getMonth() + 1,
            dayNow = dateNow.getDate() - 1,
            maxDate = yearNow + '-' + addZero(monthNow) + '-' + addZero(dayNow);
        let inp = document.querySelector('#startDate');
        let inp2 = document.querySelector('#endDate');
        inp.setAttribute('max', maxDate);
        inp2.setAttribute('max',maxDate);

    </script>
}

demo

“在此处输入图像说明”

[HttpPost]
        public IActionResult Time(Time modeo)
        {
            if (ModelState.IsValid)
            {
                // add your code here.....
                
                
            }
            //return model and error message
           return View(modeo);
        }

You can use fluent validation to achieve this. I write a simple demo here and hope it can help you.

Demo

 public class Time
    {
        public DateTime Start { get; set; }
        public DateTime End  { get; set; }
    }

DateTimeValidator

public class DateTimeValidator : AbstractValidator<Time>
    {
      
        public DateTimeValidator()
        {
            DateTime now = DateTime.Now;

            RuleFor(x => x.Start)
                .LessThan(x => x.End)
                .WithMessage("Start must < End")
                .NotEmpty(); 

            RuleFor(x => x.End)
                .GreaterThan(x => x.Start)
                .WithMessage("End must > Start")
                .NotEmpty();
        }
    }

Page

@model Time

<form asp-action="Time" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Start" class="control-label" ></label>
                <input asp-for="Start" class="form-control" type="date" max='' id="startDate"/>
                <span asp-validation-for="Start" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="End" class="control-label"></label>
                <input asp-for="End" class="form-control" type="date" max='' id="endDate"/>
                <span asp-validation-for="End" class="text-danger"></span>
            </div>
            <button type="submit">submit</button>
</form>

@section  Scripts{
    <script>
        //I use js to allow users to select only the latest date up to yesterday
        function addZero(n) {
            return parseInt(n) >= 10 ? n.toString() : '0' + n;
        }
        let dateNow = new Date(),
            yearNow = dateNow.getFullYear(),
            monthNow = dateNow.getMonth() + 1,
            dayNow = dateNow.getDate() - 1,
            maxDate = yearNow + '-' + addZero(monthNow) + '-' + addZero(dayNow);
        let inp = document.querySelector('#startDate');
        let inp2 = document.querySelector('#endDate');
        inp.setAttribute('max', maxDate);
        inp2.setAttribute('max',maxDate);

    </script>
}

Demo

enter image description here

Edit

[HttpPost]
        public IActionResult Time(Time modeo)
        {
            if (ModelState.IsValid)
            {
                // add your code here.....
                
                
            }
            //return model and error message
           return View(modeo);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文