We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Blasor Form Validation Library

Hello,

 Are there any examples how to use the Blasor Form Validation Library like is  advertised in Form Validation library to validate HTML form elements ?




3 Replies

BC Berly Christopher Syncfusion Team June 11, 2019 12:44 PM UTC

Hi Frangoudes, 
  
Greetings from Syncfusion. 
  
You can achieve the blazor form validation for form elements using data annotation support. We have prepared the sample for validate our EJ2 TextBox, DatePicker and NumericTextBox component using data annotation as mentioned in the below code example.  
[Pages/Index.razor] 
<EditForm Model="@employee" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit"> 
    <DataAnnotationsValidator /> 
    <div class="form-group"> 
        <label for="employee-id">Employee ID</label> 
        <EjsTextBox id="employee-id" Placeholder='Provide employee ID' bind-Value="@employee.EmpID"></EjsTextBox> 
        <ValidationMessage For="@(() => employee.EmpID)" /> 
    </div> 
    <div class="form-group"> 
        <label for="employee-name">Employee Name</label> 
        <EjsTextBox id="employee-name" Placeholder='Provide employee name' bind-Value="@employee.Name"></EjsTextBox> 
        <ValidationMessage For="@(() => employee.Name)" /> 
    </div> 
    <div class="form-group"> 
        <label for="DOB">Date of Birth</label> 
        <EjsDatePicker id="date-of-birth" Placeholder='Provide employee age older than 21yrs' bind-Value="@employee.DateOfBirth"></EjsDatePicker> 
        <ValidationMessage For="@(() => employee.DateOfBirth)" /> 
    </div> 
    <div class="form-group"> 
        <label for="salary">Salary</label> 
        <EjsNumericTextBox id="salary" Placeholder="Enter the salary" bind-Value="@employee.Salary" Step="1000"></EjsNumericTextBox> 
        <ValidationMessage For="@(() => employee.Salary)" /> 
    </div> 
    <button type="submit" class="btn btn-primary">Register</button> 
    <h2>@FormValidateState</h2> 
</EditForm> 
 
@functions { 
    public string FormValidateState = "Click Register button to validate and submit the form."; 
    public Employee employee = new Employee(); 
 
    public void HandleValidSubmit() 
    { 
        FormValidateState = "Submitted form is valid."; 
 
    } 
    // Form Validation State : @FormValidationState 
    public void HandleInvalidSubmit() 
    { 
        FormValidateState = "Submitted form is invalid."; 
 
    } 
 
    public class Employee 
    { 
        [Required(ErrorMessage = "Employee ID is required")] 
 
        public string EmpID { get; set; } 
 
        [Required(ErrorMessage = "Name is required")] 
        public string Name { get; set; } 
 
        [Required] 
        [DateOfBirthValidationAttribute(ErrorMessage = "Employee age must be older than 21yrs.")] 
        public DateTime? DateOfBirth { get; set; } = new DateTime(1998, 1, 1); 
 
        [Required] 
        [Range(5000, 10000, ErrorMessage = "Salary range is invalid (5000-10000).")] 
        public object Salary { get; set; } 
 
    } 
 
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] 
    public class DateOfBirthValidationAttribute : ValidationAttribute 
    { 
        public override bool IsValid(object value) 
        { 
            return (DateTime.Now - (DateTime)value).Days / 365 >= 21; 
        } 
    } 
} 

  
Please find the sample from the below link. 

Regards, 
Berly B.C 



FP Frangoudes Panikos June 13, 2019 04:24 PM UTC

Thank you very much Berly for your prompt reply.









BC Berly Christopher Syncfusion Team June 14, 2019 06:02 AM UTC

Hi Frangoudes, 
  
Most Welcome. We are glad to assist you. 
  
Regards, 
Berly B.C 


Loader.
Up arrow icon