Example of API in ASP.NET MVC OtpInput Control
This sample demonstrates the properties available in the OTP Input control.
Enter verification code
Styling Mode
Validation Status
Placeholder
Separator
Length
Disabled
@Html.UiOtpInput("apiOtp", new UiOtpInputOptions { Separator = "-", Placeholder = "X", OnInput = "handleOtpChange", AriaLabel = "Verification code" })
@Html.UiButton(new UiButtonOptions { Id = "reset-btn", Text = "Clear", Disabled = true })
@Html.UiButton(new UiButtonOptions { Id = "verify-btn", Text = "Verify", Color = UiButtonColor.Primary, Disabled = true })
@Html.UiDropDownList("stylingMode", new UiDropDownListOptions { Width = "120px", Value = "Outlined", OnChange = "stylingModeChange", Items = ((IEnumerable<InputsDemoController.OtpChoice>)ViewData["stylingMode"]).Select(c => new UiListItem(c.value, c.text)).ToList() })
@Html.UiDropDownList("validationStatus", new UiDropDownListOptions { Width = "120px", Value = "", OnChange = "statusChange", Items = ((IEnumerable<InputsDemoController.OtpChoice>)ViewData["validationStatus"]).Select(c => new UiListItem(c.value, c.text)).ToList() })
@Html.UiNumericTextBox("length", new UiNumericTextBoxOptions { Value = 4, Min = 1, Max = 6, Step = 1, Format = "N0", Width = "100px", OnChange = "updateOtpLength" })
@Html.UiSwitch("disabled", false, new UiSwitchOptions { Id = "disabledSwitch", OnChange = "handleDisabled" })
<style>
.property-section .property-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; }
.property-section .prop-input { width: 90px; padding: 4px 6px; border: 1px solid var(--ui-input-border); border-radius: var(--ui-radius); background: var(--ui-surface); color: var(--ui-text); font: inherit; }
.form-container { display: flex; flex-direction: column; align-items: center; gap: 16px; padding: 30px 0; }
.otp-header { font-weight: 600; }
.otp-actions { display: flex; gap: 8px; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section sb-property-border">
<div class="api-otp-wrapper">
<div id="otp-container">
<div class="form-container">
<span class="otp-header"> Enter verification code </span>
@Html.UiOtpInput("apiOtp", new UiOtpInputOptions { Separator = "-", Placeholder = "X", OnInput = "handleOtpChange", AriaLabel = "Verification code" })
<div class="otp-actions">
@Html.UiButton(new UiButtonOptions { Id = "reset-btn", Text = "Clear", Disabled = true })
@Html.UiButton(new UiButtonOptions { Id = "verify-btn", Text = "Verify", Color = UiButtonColor.Primary, Disabled = true })
</div>
<div id="otp-result" aria-live="polite"></div>
</div>
</div>
</div>
</div>
<div class="demo-property-panel property-section" title="Properties">
<div class="property-row"><span>Styling Mode</span> @Html.UiDropDownList("stylingMode", new UiDropDownListOptions { Width = "120px", Value = "Outlined", OnChange = "stylingModeChange", Items = ((IEnumerable<InputsDemoController.OtpChoice>)ViewData["stylingMode"]).Select(c => new UiListItem(c.value, c.text)).ToList() })</div>
<div class="property-row"><span>Validation Status</span> @Html.UiDropDownList("validationStatus", new UiDropDownListOptions { Width = "120px", Value = "", OnChange = "statusChange", Items = ((IEnumerable<InputsDemoController.OtpChoice>)ViewData["validationStatus"]).Select(c => new UiListItem(c.value, c.text)).ToList() })</div>
<div class="property-row"><span>Placeholder</span> <input id="placeHolder" type="text" class="prop-input" value="X" maxlength="4" /></div>
<div class="property-row"><span>Separator</span> <input id="separator" type="text" class="prop-input" value="-" maxlength="1" /></div>
<div class="property-row"><span>Length</span> @Html.UiNumericTextBox("length", new UiNumericTextBoxOptions { Value = 4, Min = 1, Max = 6, Step = 1, Format = "N0", Width = "100px", OnChange = "updateOtpLength" })</div>
<div class="property-row"><span>Disabled</span> @Html.UiSwitch("disabled", false, new UiSwitchOptions { Id = "disabledSwitch", OnChange = "handleDisabled" })</div>
</div>
<script>
// Syncfusion: the panel sets the styling mode, the validation status, the placeholder, the separator, the length and the disabled state; Verify reports the entered value
var verifyBtn = document.getElementById('verify-btn');
var resetBtn = document.getElementById('reset-btn');
function otpInput() { return window.Lumen.get('apiOtp'); }
function handleOtpChange(value) {
verifyBtn.disabled = String(value).length !== otpInput().getLength();
resetBtn.disabled = !String(value).length;
}
resetBtn.addEventListener('click', function () { otpInput().clear(); verifyBtn.disabled = resetBtn.disabled = true; document.getElementById('otp-result').textContent = ''; });
verifyBtn.addEventListener('click', function () { document.getElementById('otp-result').textContent = 'Entered OTP value is : ' + otpInput().getValue(); });
function stylingModeChange(value) { if (value) { otpInput().setStylingMode(value); } }
function statusChange(value) { otpInput().setStatus(value === 'e-success' ? 'Success' : value === 'e-warning' ? 'Warning' : value === 'e-error' ? 'Error' : ''); }
document.getElementById('placeHolder').addEventListener('input', function (e) { otpInput().setPlaceholder(e.target.value); });
document.getElementById('separator').addEventListener('input', function (e) { otpInput().setSeparator(e.target.value); });
function updateOtpLength(value) { if (value) { otpInput().setLength(value); handleOtpChange(otpInput().getValue()); } }
function handleDisabled(checked) { otpInput()[checked ? 'disable' : 'enable'](); }
</script>
<script>
(function (window, document) {
'use strict';
document.querySelector('.input-actions').addEventListener('click', function (event) {
var action = event.target.getAttribute('data-input-action');
var control = window.Lumen.get('inputApi');
if (!action || !control) { return; }
if (action === 'set') { control.setValue('1234'); }
else if (action === 'state') { window.InputSampleLog('Dummy code length: ' + control.getValue().length); return; }
else if (action === 'enabled') { control[control.isEnabled() ? 'disable' : 'enable'](); }
else if (action === 'clear') { control.clear(); }
else if (action === 'focus') { control.focus(); }
window.InputSampleLog('API action: ' + action);
});
})(window, document);
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class OtpInputController : Controller
{
// The page is Views/OtpInput/API.cshtml; it draws what ViewData holds.
public ActionResult API()
{
List<OtpChoice> stylingMode = new List<OtpChoice>();
stylingMode.Add(new OtpChoice("Outlined", "Outlined"));
stylingMode.Add(new OtpChoice("Underlined", "Underlined"));
stylingMode.Add(new OtpChoice("Filled", "Filled"));
ViewData["stylingMode"] = stylingMode;
List<OtpChoice> validationStatus = new List<OtpChoice>();
validationStatus.Add(new OtpChoice("None", ""));
validationStatus.Add(new OtpChoice("Success", "e-success"));
validationStatus.Add(new OtpChoice("Warning", "e-warning"));
validationStatus.Add(new OtpChoice("Error", "e-error"));
ViewData["validationStatus"] = validationStatus;
return View();
}
public class OtpChoice
{
public string text { get; set; }
public string value { get; set; }
public OtpChoice(string text, string value) { this.text = text; this.value = value; }
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
This sample can be customized further with the combination of OTP Input properties from the property pane. For example,
- The input style can be changed by selecting the Styling Mode dropdownlist from the property pane.
- The validation state can be changed by selecting the Validation Status dropdownlist from the property pane.
- The hint placeholder character can be updated by using the Placeholder textbox from the property pane.
- The separator character (-) can be updated by using the Separator textbox from the property pane.
- The input field length can be changed by using the Length numerictextbox from the property pane.
- Enable or Disable the OTP Input by toggling the Disabled switcher button.