{"id":58609,"date":"2023-09-26T16:06:23","date_gmt":"2023-09-26T10:36:23","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=58609"},"modified":"2023-09-28T16:32:21","modified_gmt":"2023-09-28T11:02:21","slug":"otp-verification-through-mobile-number-using-javascript","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/otp-verification-through-mobile-number-using-javascript\/","title":{"rendered":"OTP Verification through Mobile Number using Javascript"},"content":{"rendered":"<p>Verifying a One-Time Password (dru) typically involves comparing the OTP a user provides to the one generated by a trusted system or service. OTPs are commonly used for authentication and security purposes. Here&#8217;s a general outline of how to verify an OTP.<\/p>\n<h3><img decoding=\"async\" loading=\"lazy\" class=\"alignleft wp-image-58605 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/contactForm.png\" alt=\"login-form\" width=\"573\" height=\"198\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/contactForm.png 573w, \/blog\/wp-ttn-blog\/uploads\/2023\/09\/contactForm-300x104.png 300w\" sizes=\"(max-width: 573px) 100vw, 573px\" \/><\/h3>\n<h3><b>User Input<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">The user provides an OTP through an application.<\/span><\/p>\n<h3><b>Generate an OTP<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">On the server side, an OTP is generated by a third-party api by clicking the get OTP button.<\/span><\/p>\n<h3><b>Validation<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">The below steps are to validate the OTP.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. Put the OTP in the OTP field with the validate OTP button.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. If <\/span><span style=\"font-weight: 400;\">validate-otp-by-number<\/span><span style=\"font-weight: 400;\"> api returns success, then display the matched message and add a class isValidateOtp else, throw an error.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">$<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">'.validate-otp'<\/span><span style=\"font-weight: 400;\">).<\/span><span style=\"font-weight: 400;\">once<\/span><span style=\"font-weight: 400;\">().<\/span><span style=\"font-weight: 400;\">click<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> (<\/span><span style=\"font-weight: 400;\">e<\/span><span style=\"font-weight: 400;\">) {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0var<\/span> <span style=\"font-weight: 400;\">mobile_element<\/span><span style=\"font-weight: 400;\"> = <\/span><span style=\"font-weight: 400;\">$<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">'.mobile-number<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0var<\/span> <span style=\"font-weight: 400;\">mobileNumber<\/span><span style=\"font-weight: 400;\"> = <\/span><span style=\"font-weight: 400;\">mobile_element<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">val<\/span><span style=\"font-weight: 400;\">();<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0var<\/span> <span style=\"font-weight: 400;\">otp<\/span><span style=\"font-weight: 400;\"> = <\/span><span style=\"font-weight: 400;\">$<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">'.otp'<\/span><span style=\"font-weight: 400;\">).<\/span><span style=\"font-weight: 400;\">val<\/span><span style=\"font-weight: 400;\">();<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0var<\/span> <span style=\"font-weight: 400;\">data<\/span><span style=\"font-weight: 400;\"> = [<\/span><span style=\"font-weight: 400;\">otp<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">mobileNumber<\/span><span style=\"font-weight: 400;\">];<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0if<\/span><span style=\"font-weight: 400;\"> (<\/span><span style=\"font-weight: 400;\">\/<\/span><span style=\"font-weight: 400;\">^<\/span><span style=\"font-weight: 400;\">\\d<\/span><span style=\"font-weight: 400;\">{5,}<\/span><span style=\"font-weight: 400;\">$<\/span><span style=\"font-weight: 400;\">\/<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">test<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">mobile_element<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">val<\/span><span style=\"font-weight: 400;\">())) {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0$<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">'.error-otp<\/span><span style=\"font-weight: 400;\">).<\/span><span style=\"font-weight: 400;\">empty<\/span><span style=\"font-weight: 400;\">();<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0$<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">ajax<\/span><span style=\"font-weight: 400;\">({<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0type:<\/span> <span style=\"font-weight: 400;\">'POST'<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0url:<\/span> <span style=\"font-weight: 400;\">'\/validate-otp-by-number\/'<\/span><span style=\"font-weight: 400;\"> + <\/span><span style=\"font-weight: 400;\">data<\/span><span style=\"font-weight: 400;\">, <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0dataType:<\/span> <span style=\"font-weight: 400;\">\"json\"<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cache:<\/span> <span style=\"font-weight: 400;\">false<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0success<\/span><span style=\"font-weight: 400;\">:<\/span> <span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> (<\/span><span style=\"font-weight: 400;\">result<\/span><span style=\"font-weight: 400;\">) {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">result<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">status<\/span><span style=\"font-weight: 400;\"> == <\/span><span style=\"font-weight: 400;\">'success'<\/span><span style=\"font-weight: 400;\">) {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">'.otp'<\/span><span style=\"font-weight: 400;\">).<\/span><span style=\"font-weight: 400;\">addClass<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">'isValidateOtp'<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">$<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">'.otp'<\/span><span style=\"font-weight: 400;\">).<\/span><span style=\"font-weight: 400;\">append<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">'&lt;span class=\"green\"&gt;'<\/span><span style=\"font-weight: 400;\">+<\/span><span style=\"font-weight: 400;\">result<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">message<\/span><span style=\"font-weight: 400;\">+<\/span><span style=\"font-weight: 400;\">'&lt;\/span&gt;'<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">{<\/span>\r\n<span style=\"font-weight: 400;\">$<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">'.otp'<\/span><span style=\"font-weight: 400;\">).<\/span><span style=\"font-weight: 400;\">append<\/span><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">'&lt;span class=\"error\"&gt;'<\/span><span style=\"font-weight: 400;\">+<\/span><span style=\"font-weight: 400;\">result<\/span><span style=\"font-weight: 400;\">.<\/span><span style=\"font-weight: 400;\">message<\/span><span style=\"font-weight: 400;\">+<\/span><span style=\"font-weight: 400;\">'&lt;\/span&gt;'<\/span><span style=\"font-weight: 400;\">);<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0});<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">});\r\n<\/span><\/pre>\n<p><span style=\"font-weight: 400;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft wp-image-58606 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/otp-macthed.png\" alt=\"otp match\" width=\"578\" height=\"213\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/otp-macthed.png 578w, \/blog\/wp-ttn-blog\/uploads\/2023\/09\/otp-macthed-300x111.png 300w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\"><strong>3.<\/strong> If the user changes the number, then remove the isValidateOtp class\u00a0<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0jQuery('.mobile-number').change(function(){\r\n<\/span><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$('.otp').removeClass('isValidateOtp');\r\n<\/span><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0})<\/span><span style=\"font-weight: 400;\">\r\n<\/span><\/pre>\n<h3><img decoding=\"async\" loading=\"lazy\" class=\"alignleft wp-image-58608 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/validate-opt.png\" alt=\"validate-otp-erro\" width=\"620\" height=\"221\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/validate-opt.png 620w, \/blog\/wp-ttn-blog\/uploads\/2023\/09\/validate-opt-300x107.png 300w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><\/h3>\n<h3><b>Conclusion<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">The above article is used for the OTP validation and to prevent access to the other accounts.<\/span><\/p>\n<p>Thanks for reading this article, Hope, you will find this helpful. Please comment below if you have any questions regarding the same.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Verifying a One-Time Password (dru) typically involves comparing the OTP a user provides to the one generated by a trusted system or service. OTPs are commonly used for authentication and security purposes. Here&#8217;s a general outline of how to verify an OTP. User Input The user provides an OTP through an application. Generate an OTP [&hellip;]<\/p>\n","protected":false},"author":1546,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":865},"categories":[3602,3429],"tags":[4862,4860,4857,2591],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58609"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1546"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=58609"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58609\/revisions"}],"predecessor-version":[{"id":58839,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58609\/revisions\/58839"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=58609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=58609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=58609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}