- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 01:10 PM
I'm trying to get a regex for a masked field that I want to require that there are at least 3 different characters in the entry. Does anyone have a suggestion?
So an entry of just 1s and 2s would fail, like '1221121' but a third char would pass '1221123'
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 01:56 PM
@Travis Michigan
Please try the below snippet:
function hasAtLeastThreeUniqueCharacters(input) {
var uniqueCharacters = {}; // store unique characters
var count = 0; // Counter for unique characters
for (var i = 0; i < input.length; i++) {
var character = input[i];
if (!uniqueCharacters.hasOwnProperty(character)) {
uniqueCharacters[character] = true; // Mark this character as encountered
count++; // Increment count for each unique character
}
}
return count >= 3; // Check if there are at least three unique characters
}
// Example usage
gs.print(hasAtLeastThreeUniqueCharacters('1221123')); // true
gs.print(hasAtLeastThreeUniqueCharacters('1221121')); // false
gs.print(hasAtLeastThreeUniqueCharacters('1234567')); // true
gs.print(hasAtLeastThreeUniqueCharacters('aabbcc')); // true
gs.print(hasAtLeastThreeUniqueCharacters('aaaaabb')); // false
gs.print(hasAtLeastThreeUniqueCharacters('abcde')); // true
The above method is to get the count of distinct characters and if its greater or equal to 3 it will return true.
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 01:56 PM
@Travis Michigan
Please try the below snippet:
function hasAtLeastThreeUniqueCharacters(input) {
var uniqueCharacters = {}; // store unique characters
var count = 0; // Counter for unique characters
for (var i = 0; i < input.length; i++) {
var character = input[i];
if (!uniqueCharacters.hasOwnProperty(character)) {
uniqueCharacters[character] = true; // Mark this character as encountered
count++; // Increment count for each unique character
}
}
return count >= 3; // Check if there are at least three unique characters
}
// Example usage
gs.print(hasAtLeastThreeUniqueCharacters('1221123')); // true
gs.print(hasAtLeastThreeUniqueCharacters('1221121')); // false
gs.print(hasAtLeastThreeUniqueCharacters('1234567')); // true
gs.print(hasAtLeastThreeUniqueCharacters('aabbcc')); // true
gs.print(hasAtLeastThreeUniqueCharacters('aaaaabb')); // false
gs.print(hasAtLeastThreeUniqueCharacters('abcde')); // true
The above method is to get the count of distinct characters and if its greater or equal to 3 it will return true.
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 02:19 PM
Thank you @Community Alums !