Last active
February 2, 2024 15:30
-
-
Save step307/3d265b7c7cb4eccdf0cf55a68c9cfefa to your computer and use it in GitHub Desktop.
Javascript check salesforce id validity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isValidSfId(str) { | |
// https://stackoverflow.com/a/29299786/1333724 | |
if (typeof str !== 'string' || str.length !== 18) { | |
return false; | |
} | |
let upperCaseToBit = (char) => char.match(/[A-Z]/) ? '1' : '0'; | |
let binaryToSymbol = (digit) => digit <= 25 ? String.fromCharCode(digit + 65) : String.fromCharCode(digit - 26 + 48); | |
let parts = [ | |
str.slice(0,5).split("").reverse().map(upperCaseToBit).join(""), | |
str.slice(5,10).split("").reverse().map(upperCaseToBit).join(""), | |
str.slice(10,15).split("").reverse().map(upperCaseToBit).join(""), | |
]; | |
let check = parts.map(str => binaryToSymbol(parseInt(str, 2))).join(""); | |
return check === str.slice(-3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Organization IDs are mostly 15 characters, and is one of the most important IDs to validate? Is there any way to do that?