|
1 | | -// Alphanumeric regex pattern |
2 | | -var alphanumericPattern = /^[a-zA-Z0-9]*$/; |
3 | 1 |
|
4 | | -// Function to validate if a string is alphanumeric |
5 | | -function isAlphanumeric(str) { |
6 | | - return alphanumericPattern.test(str); |
7 | | -} |
| 2 | +function isValidGSTNo(str) { |
| 3 | + // Regex to check valid GST CODE |
| 4 | + var regex = /^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/; |
| 5 | + |
| 6 | + // GST CODE is empty return false |
| 7 | + if (str == null) { |
| 8 | + return "false"; |
| 9 | + } |
8 | 10 |
|
9 | | -// Example usage |
10 | | -var examples = [ |
11 | | - "abc123", // Valid |
12 | | - "ABC", // Valid |
13 | | - "123", // Valid |
14 | | - "abc123!", // Invalid (contains '!') |
15 | | - "hello world", // Invalid (contains space) |
16 | | - "123-456", // Invalid (contains '-') |
17 | | -]; |
18 | | - |
19 | | -// Test the examples using a for loop |
20 | | -for (var i = 0; i < examples.length; i++) { |
21 | | - var example = examples[i]; |
22 | | - if (isAlphanumeric(example)) { |
23 | | - gs.print(example+" is a valid alphanumeric string."); |
24 | | - } else { |
25 | | - gs.print(example+ " is NOT a valid alphanumeric string."); |
| 11 | + // Return true if the GST_CODE matched the ReGex |
| 12 | + if (regex.test(str) == true) { |
| 13 | + return "true"; |
| 14 | + } |
| 15 | + else { |
| 16 | + return "false"; |
26 | 17 | } |
27 | 18 | } |
28 | 19 |
|
| 20 | +// Test Case 1: |
| 21 | +var str1 = "33AAACH1645P2ZH"; |
| 22 | +gs.print(str1 +' isValidGSTNo '+isValidGSTNo(str1)); |
| 23 | + |
| 24 | +// Test Case 2: |
| 25 | +var str2 = "06BZAF67"; |
| 26 | +gs.print(str2 +' isValidGSTNo '+isValidGSTNo(str2)); |
| 27 | + |
| 28 | +// Test Case 3: |
| 29 | +var str3 = "AZBZAHM6385P6Z2"; |
| 30 | +gs.print(str3 +' isValidGSTNo '+isValidGSTNo(str3)); |
| 31 | + |
| 32 | +// Test Case 4: |
| 33 | +var str4 = "36AAICG1508J1ZN"; |
| 34 | +gs.print(str4 +' isValidGSTNo '+isValidGSTNo(str4)); |
| 35 | + |
| 36 | +// Test Case 5: |
| 37 | +var str5 = "27AAUCS0795B1Z0"; |
| 38 | +gs.print(str5 +' isValidGSTNo '+isValidGSTNo(str5)); |
| 39 | + |
| 40 | + |
29 | 41 | /* |
30 | | -when you run this code, it will output: |
31 | | -*** Script: abc123 is a valid alphanumeric string. |
32 | | -*** Script: ABC is a valid alphanumeric string. |
33 | | -*** Script: 123 is a valid alphanumeric string. |
34 | | -*** Script: abc123! is NOT a valid alphanumeric string. |
35 | | -*** Script: hello world is NOT a valid alphanumeric string. |
36 | | -*** Script: 123-456 is NOT a valid alphanumeric string. |
| 42 | +When you run this code, it will output: |
| 43 | +*** Script: 33AAACH1645P2ZH isValidGSTNo true |
| 44 | +*** Script: 06BZAF67 isValidGSTNo false |
| 45 | +*** Script: AZBZAHM6385P6Z2 isValidGSTNo false |
| 46 | +*** Script: 36AAICG1508J1ZN isValidGSTNo true |
| 47 | +*** Script: 27AAUCS0795B1Z0 isValidGSTNo true |
37 | 48 | */ |
0 commit comments