Validate OTP using JavaScript in ASP.Net

Last Reply one month ago By pandeyism

Posted one month ago

i want to validate my otp password. its has to be validate while entering the password. i have 4 digit otp password. i stored that password in session. then after receiving that password i have to enter that in textbox. now this textbox should be validate on pressing the 4th digit. once i entered the 4th digit it has to be validate automatically and say it is correct password or wrong password by using image.

Posted one month ago

Hi sambath,

Please refer below sample.

HTML

<script type="text/javascript">
    function Check() {
        var message = '<%=Session["otp"] %>';
        var otp = document.getElementById('txtOTP').value;
        var countdigit = otp.length;
        if (countdigit == 4) {
            if (otp == message) {
                document.getElementById('lblMessage').innerHTML = "Correct OTP";
            }
            else {
                document.getElementById('lblMessage').innerHTML = "Incorrect OTP";
            }
        }
        else {
            document.getElementById('lblMessage').innerHTML = "";
        }
        return false;
    }
</script>
<div>
    Enter OTP:
    <asp:TextBox ID="txtOTP" class="otp" runat="server" MaxLength="4" TextMode="Password"
        onkeyup="Check();" />
    <br />
    <asp:Label ID="lblMessage" runat="server" />
</div>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        Session["Otp"] = "1234";
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Session("Otp") = "1234"
    End If
End Sub

Screenshot