ASP.NET implements credit card check and custom confirmation control (1)

zhaozj2021-02-16  53

ASP.NET implements credit card check and customization

Confirm control

ASP.NET provides a very powerful confirmation control, we can use easy to use, but some special confirmation requires us to handle it, we can also inherit the confirmation control of the coupled needs from the base class of confirmation controls. this is a very practical way, here I am to tell you about two articles in this area, they provide with full implementation steps, I do not think the author of the article better than that of the entry:. David J. Gottlieb translation : Raozr

When you purchase a product on the Internet, your credit card you pay needs to be confirmed by a special program. One of them is that the credit card number and the type are valid. In most cases, this LUHN algorithm is used to implement of.

In this article, we will build a new class for validity checks for credit card numbers. In this process, we will rebate a boolean value to check. Before writing code, look at the algorithm.

From the second bit of the right to the last bit, multiply each one by one by 2. See if they add and whether it is greater than 10. If it is greater than 10, we will add the number of the right to the left, The number is added to the front and. Then, each phase is added to the final equation, such as the first, third, and 5th, etc., complete the above steps, we add all the numbers to be added. If and can be 10, then the card number is valid; vice versa. There is a simple example here:

Assume that the credit card number is

411-1111-1111-1111

4 1 1 1 - 1 1 1 1 - 1 1 1 1 - 1 1 1 1x2 x2 x2 x2 x2 x2 x2 x2 ------------------------ -------------- 8 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 When we add these numbers equal to 30.30 can be removed by 10, thereby illustrating this card number is effective Let's start writing code below.

Let's start with a Boolean return value that is a valid boolean to establish a certainty card number.

Pre.showcode {background-color: #ffff; padding-right: 3px; border: 0px; font-size: 11px; padding-bottom: 0px; padding-top: 0px; font-family: "Courier new";

.Colorlinenumber {color: red;}

.Defaultfontface {color: arian}

.Defaultfontsize {size: 3;

.Colorbackgroundline {color: #eeeeec;

.COLORALTERNATINGBACKGROUNDLINE {Color: #fffee;}

.Colorcomment {color: green;

.Colorserversidetag {color: green;

.Colordatabase {color: dodgerblue;

.ColorControlTag {color: maroon;

.Colorasptemplatetag {color: green;

.Colorstarthtmltag {color: blue;}

.Colorendhtmltag {color: blue;

.Colorvbkeyword {color: blue;

.COLORVBCOMMENT {color: green;

.Colorcskeyword {color: blue;

.Colorsqlkeyword {color: blue;

.Colorcscomment {color: green;

.Colorsqlcomment {color: green;

.Colorsqlfunctions {color: maroon;

.Colorsqldattypes {color: green;

1: Private Bool Validatecc (String Creditno) 2: {3: INT INDICATOR = 1; // - Tag 4: INT FirstnumToadd = 0; // - Used to store the first set of numbers 5: INT SecondNumToadd = 0; // - Used to store the second set of numbers and 6: String Num1; // - Used to store the leftmost integer when each digit is added and greater than 10 7: 8: String Num2; // - For every one of the digits and greater than 10, store the rightmost integer 9: 10: 10: 10: 11: // - Convert the credit card number string to one Characters 12: char [] ccarr = CreditNO.ToChararray (); 13: 14: for (int i = ccarr.length-1; i> = 0; i -) 15: {16: char ccnoadd = ccarr [i ]; 17: int ccadd = int32.Parse (ccNoadd.toString ()); 18: if (indeicator == 1) 19: {20: // - Add it to the total value when we encounter odd bits In 21:22: FirstNumToadd = CCADD; 23: // - Set the tag to 0 so that the next loop skips this block 24: 25: Indicator = 0; 26:} 27: Else28: {29: / / - If the current integer is more than 10 after 2, it will be divided into two integers and add them, 30: // - Add value to the total value. 31: 32: IF ( (CCADD CCADD)> = 1 0) 33: {34: int Temporary = (CCADD CCADD); 35: Num1 = Temporary.toString (). Substring (0, 1); 36: Num2 = Temporary.toString (). Substring (1, 1); 37: SecondNumToadd = (Convert.Toint 32 (Num1) Convert.Toint32 (Num2)); 38:} 39: Else40: {41: // - Otherwise, add them, add value to the total Value .42: SecondNUMTOADD = CCADD CCADD; 43:} 44: // - Set the tag to 1, we can perform different code to the next integer 45: 46: Indicator = 1; 47:} 48 :} 49: // - If the two numbers are 10, the card is effective, otherwise, it is invalid. 50: 51: bool isvalid = false;

52: IF (FIRSTNUMTOADD SECONDNUMTOADD)% 10 == 0) 53: {54: isvalid = true; 55:} 56: Else57: {58: isvalid = false; 59:} 60: return isvalid; 61:} We put the above programs into the Page Loag event, in this event we reference the above code, and transfer our credit card as a parameter. Pre.Showcode {Background-Color: #ffffff; padding-right: 3px; Border: 0px; font-size: 11px; padding-bottom: 0px; padding-top: 0px; font-family: "Courier new";

.Colorlinenumber {color: red;}

.Defaultfontface {color: arian}

.Defaultfontsize {size: 3;

.Colorbackgroundline {color: #eeeeec;

.COLORALTERNATINGBACKGROUNDLINE {Color: #fffee;}

.Colorcomment {color: green;

.Colorserversidetag {color: green;

.Colordatabase {color: dodgerblue;

.ColorControlTag {color: maroon;

.Colorasptemplatetag {color: green;

.Colorstarthtmltag {color: blue;}

.Colorendhtmltag {color: blue;

.Colorvbkeyword {color: blue;

.COLORVBCOMMENT {color: green;

.Colorcskeyword {color: blue;

.Colorsqlkeyword {color: blue;

.Colorcscomment {color: green;

.Colorsqlcomment {color: green;

.Colorsqlfunctions {color: maroon;

.Colorsqldattypes {color: green;

1: Private void page_load (Object sender, system.eventargs e) 2: {3: IF (page.text) 6: {7: result.text = "IS Valid! "; 8:} 9: Else10: {11: Result.text =" NOPE! "; 12:} 13:} 14:} 15:

Next: Custom controls for implementing credit card check

转载请注明原文地址:https://www.9cbs.com/read-23624.html

New Post(0)