File tree Expand file tree Collapse file tree
Specialized Areas/Regular Expressions/Indian GSTIN Validator Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // Alphanumeric regex pattern
2+ var alphanumericPattern = / ^ [ a - z A - Z 0 - 9 ] * $ / ;
3+
4+ // Function to validate if a string is alphanumeric
5+ function isAlphanumeric ( str ) {
6+ return alphanumericPattern . test ( str ) ;
7+ }
8+
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." ) ;
26+ }
27+ }
28+
29+ /*
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.
37+ */
You can’t perform that action at this time.
0 commit comments