Skip to content

Commit 986a78e

Browse files
authored
Refactor/regex10digit (#2062)
* Update script.js - Renamed variable reg to digitLengthRegex to better reflect its purpose. - Replaced string-based regex ('/^\d{10}$/') with a proper RegExp object. - Added test cases for strings with 9, 10, and 11 digits to demonstrate expected behavior. - Improved inline comments. * Update README.md Updated the readme to be more descriptive.
1 parent 6190603 commit 986a78e

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
Script is used to check if the number has 10 digts( you can update the digit count in the code based on the need.
1+
# Digit Length Validator
2+
3+
## Description
4+
5+
This script checks if a string contains exactly a specified number of digits. Useful for validating numeric input. The digit count can be adjusted in the code.
6+
7+
## Usage
8+
To change the required digit count, update the number in the regular expression
9+
```
10+
var digitLengthRegex = /^\d{N}$/; // Replace N with desired digit count
11+
```
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
var reg = '/^\d{10}$/'; // Update the number based on the need
1+
var digitLengthRegex = /^\d{10}$/; // Matches exactly 10 digits
22

3-
var k = '123456789144'; // example
3+
var elevenString = '01234567899'; // 11 digits
4+
var tenString = '0123456789'; // 10 digits
5+
var nineString = '012345678'; // 9 digits
46

5-
if (/^\d{10}$/.test(k)){ // This will check if it has 10 digits
6-
7-
}
7+
gs.info(digitLengthRegex.test(elevenString)); // false
8+
gs.info(digitLengthRegex.test(tenString)); // true
9+
gs.info(digitLengthRegex.test(nineString)); // false

0 commit comments

Comments
 (0)