How to Implement the JavaScript text message Verification Code

Source: Internet
Author: User

How to Implement the JavaScript text message Verification Code

We often need text message verification codes to ensure the integrity and correctness of our services when using online business offices of mobile and telecom carriers. Recently, a similar function has been implemented because of the business needs of a province.

The principle is very simple, that is, when a user clicks "get verification code", Ajax gets a string of Fixed-digit numbers, then writes a Database Text message, and writes a Cookie to set the validity period of the verification code.

The JS request verification code is as follows:

$. Ajax ({type: "GET", url: ".../Ajax/smsrandcodetest. ashx? Phone = "+ phone. val () + "& smsCodeRand =" + num, success: function (result) {if (result = "Y") {alert ("the verification code has been sent to your entered mobile phone number! Valid for 5 minutes "); RemainTime ();} else {alert (" failed to get the verification code! Please obtain ") ;}}, error: function () {alert (" error ") ;}}); // obtain the 6-digit random verification code function random () {var num = ""; for (I = 0; I <6; I ++) {num = num + Math. floor (Math. random () * 10);} return num;} // Verification Code validity period countdown function RemainTime () {var iSecond; var sSecond = "", sTime = ""; if (iTime> = 0) {iSecond = parseInt (iTime % 300); if (iSecond> = 0) {sSecond = iTime + "seconds ";} sTime = "<span style = 'color: darkorange; font-size: 13px; '>" + sSecond + "</span>"; if (iTime = 0) {clearTimeout (Account); sTime = "<span style = 'color: red; font-size: 12px; '> Verification Code expired </span> ";} else {Account = setTimeout ("RemainTime ()", 1000) ;}itime = iTime-1 ;}$ ("# endtime" ).html (sTime );}

The front-end processing is basically the same as above. Now we need to add logic to HttpHandler. To prevent the verification code generated by Js from being inconsistent with the rules, we will generate it again at the backend:

If (smscoderand. Length! = 6) // If the random code generated by JS does not match, use C # To generate random code {smscoderand = GetRandom () ;}// to write text message data and send SMS // to write Cookie, set the verification code validity period, for example, 5 minutes // Note: if all the above operations are successful, return "Y", processing failed, return "N"

For convenience, the verification code validity period is verified using cookies. When the service is submitted, the Cookie of the client is obtained to check whether the Cookie exists. If it does not exist, the Cookie is expired. If you expand your business in the future, you may consider adding the database validity period verification and some other rules, such as limiting the number of verification codes sent within one hour or one day (You cannot send text messages without limit) and so on.

The above is a detailed description of how to implement the JavaScript text message verification code. I hope it will help you.

Articles you may be interested in:
  • Code for sending a text message verification code using javascript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.