Tải bản đầy đủ (.docx) (2 trang)

Tạo Control để kiểm tra Credit Card (ASP.NET + DLL)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (54.17 KB, 2 trang )

Tạo Control để kiểm tra Credit Card (ASP.NET + DLL)
Đầu tiên chúng ta sẽ tạo một lớp bao gồm các hàm chính của chúng ta (thuật toán để nhận diện số
credit card chúng tôi đã trình bày ở phần trước)
1: using System;
2: using System.Web.UI;
3: using System.Web.UI.WebControls;
4:
5: namespace CustomValidators
6: {
7: /// <summary>
8: /// Summary description for Class1.
9: /// </summary>
10:
11:
12: public class CreditCardValidator : BaseValidator
13: {
14: protected override bool EvaluateIsValid()
15: {
16: //-- Lấy giá trị
17: string valueToValidate = this.GetControlValidationValue(this.ControlToValidate);
18: int indicator = 1;
19: int firstNumToAdd = 0;
20: int secondNumToAdd = 0;
21: string num1;
22:
23: string num2;
24:
25:
26: //-- chuyển số credit sang mảng ký tự
27: char[] ccArr = valueToValidate.ToCharArray();
28:


29: for (int i=ccArr.Length-1;i>=0;i--)
30: {
31: char ccNoAdd = ccArr[i];
32: int ccAdd = Int32.Parse(ccNoAdd.ToString());
33: if (indicator == 1)
34: {
35:
36: firstNumToAdd += ccAdd;
37:
38: indicator = 0;
39: }
40: else
41: {
42:
43:
44:
45: if ((ccAdd + ccAdd) >= 10)
46: {
47: int temporary = (ccAdd + ccAdd);
48: num1 = temporary.ToString().Substring(0,1);
49: num2 = temporary.ToString().Substring(1,1);
50: secondNumToAdd += (Convert.ToInt32(num1) + Convert.ToInt32(num2));
51: }
52: else
53: {
54:
55: secondNumToAdd += ccAdd + ccAdd;
56: }
57:
58:

59: indicator = 1;
60: }
61: }
62:
63:
64: bool isValid = false;
65: if ((firstNumToAdd + secondNumToAdd) % 10 == 0)
66: {
67: isValid = true;
68: }
69: else
70: {
71: isValid = false;
72: }
73: return isValid;
74: }
75: }
76: }
77:
Biên dịch thành Dll:
csc /target:library /out:c:\inetpub\wwwroot\bin\Validator.dll *.cs /r:System.dll,System.Web.dll
Sử dụng Dll trong dự án của chúng ta
1: <%@ Register TagPrefix=''custom'' Namespace=''CustomValidators'' Assembly=''Validator'' %>
2:
3: <html>
4: <body>
5: <form runat=''server''>
6: <asp:TextBox ID=''CCNumber'' Runat=''server'' />
7: <custom:CreditCardValidator
8: ControlToValidate=''CCNumber''

9: Runat=''server''
10: ErrorMessage=''Credit Card Number Invalid!'' />
11: <asp:Button Text=''Validate'' Runat=''server'' />
12: </form>
13: </body>
14: </html>

×