Search code examples
asp.net-coreasp.net-core-mvcunobtrusive-validation

How to trigger ASP .NET Core MVC client-side validation without submitting


How do you trigger client-side validation in ASP .NET Core MVC without submitting a form?

I am using modals, and need to trigger validation (without submitting the form).

In jQuery, I could do this

$('#formID').validate().form()

However, I would like to trigger the unobtrusive validation included in ASP .NET Core.


Solution

  • I think we can use this code:

    public class Movie
    {
        public int Id { get; set; }
    
        [Required]
        [StringLength(100)]
        public string Title { get; set; }
    
        [ClassicMovie(1960)]
        [DataType(DataType.Date)]
        [Display(Name = "Release Date")]
        public DateTime ReleaseDate { get; set; }
    
        [Required]
        [StringLength(1000)]
        public string Description { get; set; }
    
        [Range(0, 999.99)]
        public decimal Price { get; set; }
    
        public Genre Genre { get; set; }
    
        public bool Preorder { get; set; }
    }
    

    Please check this article