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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Blasor Form Validation Library

Thread ID:

Created:

Updated:

Platform:

Replies:

145156 Jun 10,2019 03:02 PM UTC Jun 14,2019 06:02 AM UTC ASP.NET Core - EJ 2 3
loading
Tags: FormValidator
Frangoudes Panikos
Asked On June 10, 2019 03:02 PM UTC

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 ?




Berly Christopher [Syncfusion]
Replied On 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 


Frangoudes Panikos
Replied On June 13, 2019 04:24 PM UTC

Thank you very much Berly for your prompt reply.








Berly Christopher [Syncfusion]
Replied On June 14, 2019 06:02 AM UTC

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon