Step 1: Include the Library
Download the library files and include them in your project. You can use the UMD build for browser compatibility or the ES module version for modern JavaScript projects.
<!-- Include CSS -->
<link rel="stylesheet" href="license-validator.min.css">
<!-- Include JavaScript -->
<script src="license-validator.min.js"></script>
Step 2: Create a Validator Instance
Configure your validator with format rules, checksum algorithm, and optional signature verification. Match these settings with your key generation configuration.
const validator = LicenseValidator.create({
format: {
groups: 4,
groupLength: 4,
separator: '-'
},
checksum: {
algorithm: 'crc32',
position: 'suffix'
},
signature: {
enabled: true,
secret: 'your-secret-key'
}
});
Step 3: Validate License Keys
Use the validate method to check license keys. The result includes validation status, parsed metadata, and detailed error messages if validation fails.
const result = validator.validate('XXXX-XXXX-XXXX-XXXX');
if (result.valid) {
console.log('License valid!', result.metadata);
// Grant access to features
} else {
console.error('Invalid license:', result.error);
// Show error message to user
}
Step 4: Use the UI Widget (Optional)
Mount a ready-to-use validation widget with a single method call. The widget includes an input field, validation button, and real-time feedback. Fully customizable and accessible.
LicenseValidator.mount('#license-input-container', {
validator: validator,
onValid: function(key, metadata) {
// Handle valid key
},
onInvalid: function(key, error) {
// Handle invalid key
}
});
Step 5: Configure Blacklists and Allowlists
Set up blacklists to revoke compromised keys or allowlists for beta testers. You can update these lists dynamically without regenerating keys.
// Add keys to blacklist
validator.setBlacklist(['XXXX-XXXX-XXXX-XXXX', 'YYYY-YYYY-YYYY-YYYY']);
// Add keys to allowlist (only these keys will be valid)
validator.setAllowlist(['ZZZZ-ZZZZ-ZZZZ-ZZZZ']);
// Add prefix-based blacklist
validator.setBlacklistPrefixes(['TEST-', 'DEMO-']);
Step 6: Handle Expiration and Metadata
Check expiration dates and extract embedded metadata like product ID, edition, and activation limits. Use this information to control feature access and display license information to users.
const result = validator.validate(key);
if (result.valid && result.metadata) {
const { productId, edition, expiry, maxActivations } = result.metadata;
// Check expiration
if (expiry && new Date(expiry) < new Date()) {
console.warn('License expired');
}
// Check activation limit
if (maxActivations && currentActivations >= maxActivations) {
console.warn('Maximum activations reached');
}
// Grant features based on edition
if (edition === 'premium') {
enablePremiumFeatures();
}
}