|
| 1 | +--- |
| 2 | +title: Avoid Validation When Typing |
| 3 | +description: how to make Telerik textbox validate not immediately when typing but when blurred |
| 4 | +type: troubleshooting |
| 5 | +page_title: Do not validate when typing, but on blur |
| 6 | +slug: textbox-validate-on-change |
| 7 | +position: |
| 8 | +tags: |
| 9 | +ticketid: 1448879 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>TextBox for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +Why does the TelerikTextBox activate the `<ValidationSummary />` while typing in the field? |
| 27 | + |
| 28 | +Is there a way to disable this behaviour? |
| 29 | + |
| 30 | +## Cause\Possible Cause(s) |
| 31 | + |
| 32 | +We believe that firing the validation immediately makes the user experience more fluid and lets the user know about form issues quickly, which reduces frustration. Thus, we fire validation with the `ValueChanged` event. |
| 33 | + |
| 34 | +## Solution |
| 35 | + |
| 36 | +A way to change the default behavior would be to: |
| 37 | + |
| 38 | +1. Remove the two-way bindig (`@bind-Value` -> `Value`), |
| 39 | +1. use the `OnChange` event of the Telerik component to alter the model value, |
| 40 | +1. re-validate the form by using an `EditContext` object. |
| 41 | + |
| 42 | +>caption Fire Validataion on Blur and Enter with Telerik Textbox |
| 43 | +
|
| 44 | +```CSHTML |
| 45 | +@using System.ComponentModel.DataAnnotations |
| 46 | +
|
| 47 | +from model: @person.theTbValue |
| 48 | +
|
| 49 | +<EditForm EditContext="@MyEditContext" OnValidSubmit="@OnValidSubmit" OnInvalidSubmit="@OnInvalidSubmit"> |
| 50 | + <DataAnnotationsValidator /> |
| 51 | + <ValidationSummary /> |
| 52 | +
|
| 53 | + <TelerikTextBox OnChange="@MyChangeHandler" |
| 54 | + Value="@person.theTbValue" |
| 55 | + ValueExpression="@( () => person.theTbValue )"> |
| 56 | + </TelerikTextBox> |
| 57 | +
|
| 58 | + <button type="submit">Submit</button> |
| 59 | +
|
| 60 | +</EditForm> |
| 61 | +
|
| 62 | +@logger |
| 63 | +
|
| 64 | +@code { |
| 65 | + Person person = new Person(); |
| 66 | +
|
| 67 | + EditContext MyEditContext { get; set; } |
| 68 | +
|
| 69 | + protected override void OnInitialized() |
| 70 | + { |
| 71 | + MyEditContext = new EditContext(person); |
| 72 | + } |
| 73 | +
|
| 74 | + public class Person |
| 75 | + { |
| 76 | + [Required(ErrorMessage = "Enter a name")] |
| 77 | + [StringLength(10, ErrorMessage = "That name is too long")] |
| 78 | + public string theTbValue { get; set; } |
| 79 | + } |
| 80 | +
|
| 81 | + private void MyChangeHandler(object theUserInput) |
| 82 | + { |
| 83 | + person.theTbValue = (string)theUserInput; |
| 84 | +
|
| 85 | + //you need to tell the edit context to re-validate the current data |
| 86 | + //so you need to create your own edit context, not just a model |
| 87 | + MyEditContext.Validate(); |
| 88 | + } |
| 89 | +
|
| 90 | + MarkupString logger { get; set; } |
| 91 | + void OnValidSubmit() |
| 92 | + { |
| 93 | + logger = new MarkupString(logger + $"<br />valid submit on {DateTime.Now}"); |
| 94 | + } |
| 95 | +
|
| 96 | + void OnInvalidSubmit() |
| 97 | + { |
| 98 | + logger = new MarkupString(logger + $"<br />INVALID submit on {DateTime.Now}"); |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
0 commit comments