Your search did not match any results.
Form

Validation

ASP.NET MVC

@using(Html.BeginForm()) { using(Html.DevExtreme().ValidationGroup()) { @(Html.DevExtreme().Form() .ShowValidationSummary(true) .Items(items => { items.AddGroup() .Caption("Credentials") .Items(groupItems => { groupItems.AddSimple() .DataField("Login"); groupItems.AddSimple() .DataField("Password") .Editor(e => e.TextBox().Mode(TextBoxMode.Password)); groupItems.AddSimple() .DataField("ConfirmPassword") .Editor(e => e.TextBox().Mode(TextBoxMode.Password)); }); items.AddGroup() .Caption("Personal Data") .Items(groupItems => { groupItems.AddSimple() .DataField("Name"); groupItems.AddSimple() .DataField("Date") .Label(l => l.Text("Date of birth")); }); items.AddGroup() .Caption("Billing address") .Items(groupItems => { groupItems.AddSimple() .DataField("Country") .Editor(e => e .SelectBox() .DataSource(d => d.WebApi().Controller("GeoNames").LoadAction("Countries")) ); groupItems.AddSimple() .DataField("City") .Editor(e => e .Autocomplete() .MinSearchLength(2) .DataSource(d => d.WebApi().Controller("GeoNames").LoadAction("Cities")) ); groupItems.AddSimple() .DataField("Address"); groupItems.AddSimple() .DataField("Phone") .HelpText("Enter the phone number in USA phone format") .Editor(e => e.TextBox() .Mask("+1 (X00) 000-0000") .MaskRules(new { X = new JS("/[02-9]/") }) .MaskInvalidMessage("The phone must have a correct USA phone format") .UseMaskedValue(true) ); }); items.AddSimple() .DataField("Accepted") .Label(l => l.Visible(false)) .Editor(editor => editor.CheckBox().Text("I agree to the Terms and Conditions")); }) .FormData(Model) ) <br /> @(Html.DevExtreme().Button() .Text("Register") .Type(ButtonType.Success) .UseSubmitBehavior(true) ) } } <script> function verifyAge(options) { return new Date(Date.now()).getFullYear() - options.value.getFullYear() >= 21; } </script>
using DevExtreme.AspNet.Data; using DevExtreme.AspNet.Mvc; using DevExtreme.MVC.Demos.Models.SampleData; using DevExtreme.MVC.Demos.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace DevExtreme.MVC.Demos.Controllers { public class FormController : Controller { [HttpGet] public ActionResult Validation() { return View(new EditorsViewModel() { Name = "Peter" }); } [HttpPost] public ActionResult Validation(EditorsViewModel userInfo) { if(ModelState.IsValid) { return View("SuccessValidation"); } return View(userInfo); } } }
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using System.Web.Mvc; namespace DevExtreme.MVC.Demos.ViewModels { public class AgeVerificationAttribute : ValidationAttribute, IClientValidatable { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { DateTime date = (DateTime)value; if(date <= DateTime.Now.AddYears(-21)) { return ValidationResult.Success; } return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName)); } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule(); rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()); rule.ValidationParameters.Add("validationCallback", "verifyAge"); rule.ValidationType = "custom"; yield return rule; } } }
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using System.Web.Mvc; namespace DevExtreme.MVC.Demos.ViewModels { public class EditorsViewModel { [Required(ErrorMessage = "Login is required")] public string Login { get; set; } [Required(ErrorMessage = "Name is required")] [RegularExpression(@"^[^0-9]+$", ErrorMessage = "Do not use digits in the Name.")] [StringLength(int.MaxValue, MinimumLength = 2, ErrorMessage = "Name must have at least 2 symbols")] public string Name { get; set; } [Required(ErrorMessage = "Password is required")] public string Password { get; set; } [Required(ErrorMessage = "Confirm Password is required")] [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "'Password' and 'Confirm Password' do not match.")] public string ConfirmPassword { get; set; } [RegularExpression(@"^\+\s*1\s*\(\s*[02-9]\d{2}\)\s*\d{3}\s*-\s*\d{4}$", ErrorMessage = "The phone must have a correct USA phone format")] public string Phone { get; set; } public string Extension { get; set; } [Required(ErrorMessage = "Country is required")] public string Country { get; set; } [Required(ErrorMessage = "Address is required")] public string Address { get; set; } public string Description { get; set; } public int Age { get; set; } public string Drink { get; set; } [Required(ErrorMessage = "City is required")] [RegularExpression("^[^0-9]+$", ErrorMessage = "Do not use digits in the City name.")] [StringLength(int.MaxValue, MinimumLength = 2, ErrorMessage = "City must have at least 2 symbols")] public string City { get; set; } public IEnumerable<string> Colors { get; set; } public IEnumerable<string> SelectedColors { get; set; } public string Color { get; set; } [Required(ErrorMessage = "Date of birth is required")] [AgeVerification(ErrorMessage = "You must be at least 21 years old")] public DateTime Date { get; set; } [Required(ErrorMessage = "You must agree to the Terms and Conditions")] public bool Accepted { get; set; } } }