|
| 1 | +--- |
| 2 | +title: Add Eye Icon to Reveal a TextBox Password |
| 3 | +description: Learn how to implement an eye toggle button that reveals the typed password in a Telerik TextBox for Blazor. |
| 4 | +type: how-to |
| 5 | +page_title: How to Add Eye Icon to Telerik Password TextBox in Blazor |
| 6 | +slug: textbox-kb-eye-reveal-password |
| 7 | +tags: telerik, blazor, textbox |
| 8 | +ticketid: 1673598, 1672864, 1656817, 1655436, 1640843, 1619127, 1584950 |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>TextBox for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +This KB answers the following questions: |
| 26 | + |
| 27 | +* How to add an eye icon to a text input and click the icon to show and hide the entered password? |
| 28 | +* How to implement a button that reveals the password in a Telerik TextBox on click? |
| 29 | +* How to make an eye toggle appear in a password textbox no matter if the textbox is focused or not? |
| 30 | +* How to use mouseup and mousedown events on the `TelerikSvgIcon` control to toggle a password field? |
| 31 | + |
| 32 | +## Solution |
| 33 | + |
| 34 | +1. Add a [Button]({%slug components/button/overview%}) or a [ToggleButton]({%slug togglebutton-overview%}) with the desired [icon]({%slug button-icons%}) next to the TextBox or inside the [TextBox `SuffixTemplate`]({%slug common-features/input-adornments%}). |
| 35 | +1. Use the [Button's `OnClick` event handler]({%slug button-events%}) to toggle the [TextBox `Password` parameter]({%slug components/textbox/overview%}#textbox-parameters) value. |
| 36 | +1. (optional) Instead of Button `OnClick`, use `@onmousedown` and `@onmouseup` Blazor events on a generic HTML element to toggle the TextBox `Password` parameter. This approach allows using events with Telerik components that do not expose them, for example, a [Telerik SVG icon or a font icon]({%slug common-features-icons%}). |
| 37 | +1. To use the TextBox with a reveal button inside a [Telerik Form, use a `FormItem` `Template`]({%slug form-formitems-template%}). |
| 38 | + |
| 39 | +>caption Add an eye icon to reveal a password |
| 40 | +
|
| 41 | +````RAZOR |
| 42 | +<p>Reveal password on Telerik Button OnClick</p> |
| 43 | +
|
| 44 | +<TelerikTextBox @bind-Value="@TextBoxValue1" Password="@TextBoxPassword1" Width="200px"> |
| 45 | + <TextBoxSuffixTemplate> |
| 46 | + <TelerikToggleButton Icon="@SvgIcon.Eye" |
| 47 | + OnClick="@( () => TextBoxPassword1 = !TextBoxPassword1 )" |
| 48 | + Selected="@( !TextBoxPassword1 )" /> |
| 49 | + </TextBoxSuffixTemplate> |
| 50 | +</TelerikTextBox> |
| 51 | +
|
| 52 | +<p>Reveal password on HTML element mousedown</p> |
| 53 | +
|
| 54 | +<TelerikTextBox @bind-Value="@TextBoxValue2" Password="@TextBoxPassword2" Width="200px"> |
| 55 | + <TextBoxSuffixTemplate> |
| 56 | + <span @onmousedown="@( () => TextBoxPassword2 = false )" |
| 57 | + @onmouseup="@( () => TextBoxPassword2 = true )"> |
| 58 | + <TelerikButton Icon="@SvgIcon.Eye" /> |
| 59 | + </span> |
| 60 | + </TextBoxSuffixTemplate> |
| 61 | +</TelerikTextBox> |
| 62 | +
|
| 63 | +@code { |
| 64 | + private string TextBoxValue1 { get; set; } = "abcde1"; |
| 65 | + private bool TextBoxPassword1 { get; set; } = true; |
| 66 | +
|
| 67 | + private string TextBoxValue2 { get; set; } = "zyxwv2"; |
| 68 | + private bool TextBoxPassword2 { get; set; } = true; |
| 69 | +} |
| 70 | +```` |
| 71 | + |
| 72 | +## See Also |
| 73 | + |
| 74 | +* [Input Adornments]({%slug common-features/input-adornments%}) |
| 75 | +* [Form Item Templates]({%slug form-formitems-template%}) |
0 commit comments