|
1 | 1 | part of gstvalidator;
|
2 | 2 |
|
| 3 | + |
3 | 4 | class GSTvalidator extends StatefulWidget {
|
4 | 5 | final String labelText;
|
5 | 6 |
|
@@ -36,8 +37,15 @@ class _GSTvalidatorState extends State<GSTvalidator> {
|
36 | 37 | autovalidateMode: widget.autovalidateMode,
|
37 | 38 | keyboardType: widget.keyboardType,
|
38 | 39 | validator: ((value) {
|
39 |
| - String res = validateGstinCheckDigit(widget.controller.text); |
40 |
| - return res.isEmpty ? null : res; |
| 40 | + final RegExp pattern = RegExp( |
| 41 | + r"^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$"); |
| 42 | + if (value == null || value.isEmpty) { |
| 43 | + return '! Please enter value'; |
| 44 | + } |
| 45 | + if (!pattern.hasMatch(value)) { |
| 46 | + return '! Invalid GST format'; |
| 47 | + } |
| 48 | + return null; |
41 | 49 | }),
|
42 | 50 | maxLength: widget.maxLength,
|
43 | 51 | onChanged: (text) {
|
@@ -68,35 +76,9 @@ class _GSTvalidatorState extends State<GSTvalidator> {
|
68 | 76 | return 'Please enter a value';
|
69 | 77 | }
|
70 | 78 | if (!pattern.hasMatch(value)) {
|
71 |
| - return 'Invalid GST format'; |
| 79 | + return 'Invalid input format'; |
72 | 80 | }
|
73 | 81 | return null;
|
74 | 82 | }
|
75 |
| - |
76 |
| - String validateGstinCheckDigit(String gstin) { |
77 |
| - var gstval = _validateInput(gstin); |
78 |
| - if ((gstval ?? '').toString().isNotEmpty) { |
79 |
| - return gstval.toString(); |
80 |
| - } |
81 |
| - int factor = 1; |
82 |
| - int total = 0; |
83 |
| - String codePointChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
84 |
| - int mod = codePointChars.length; |
85 |
| - String inputChars = gstin.substring(0, gstin.length - 1); |
86 |
| - |
87 |
| - for (int i = 0; i < inputChars.length; i++) { |
88 |
| - String char = inputChars[i]; |
89 |
| - int digit = factor * codePointChars.indexOf(char); |
90 |
| - digit = (digit ~/ mod) + (digit % mod); |
91 |
| - total += digit; |
92 |
| - factor = (factor == 1) ? 2 : 1; |
93 |
| - } |
94 |
| - |
95 |
| - if (gstin[gstin.length - 1] != |
96 |
| - codePointChars[((mod - (total % mod)) % mod)]) { |
97 |
| - return "The GSTIN provided is not valid; \n It fails the check digit validation."; |
98 |
| - } |
99 |
| - |
100 |
| - return ""; |
101 |
| - } |
102 | 83 | }
|
| 84 | + |
0 commit comments