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 |
![]() |
Tags: FormValidator |
<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;
}
}
} |
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.
This page will automatically be redirected to the sign-in page in 10 seconds.