how to generate the unique ID using textField
I just new in Joget and I using the textField instead of using ID Generator because it's generating the increment value in the format I gave in that. But I want to generate the value like IIFE3566 OR AEJI9732 as we open the form. here is the my code (from ChatGPT) :
here also using the url: '/jw/web/json/your-app-id/form/custom_code_check', // I don't know to whom replace this,
<script>
function generateRandomCode() {
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let randomLetters = '';
for (let i = 0; i < 4; i++) {
randomLetters += letters.charAt(Math.floor(Math.random() * letters.length));
}
const randomNumbers = Math.floor(1000 + Math.random() * 9000);
return `${randomLetters}-${randomNumbers}`;
}
function checkCodeUnique(code, callback) {
$.ajax({
url: '/jw/web/json/your-app-id/form/custom_code_check', // replace this
method: 'POST',
data: { code: code },
success: function(response) {
callback(response.isUnique === true);
},
error: function() {
callback(false);
}
});
}
function generateAndSetUniqueCode() {
let attempts = 0;
function tryGenerate() {
const code = generateRandomCode();
checkCodeUnique(code, function(isUnique) {
if (isUnique) {
$('#random_code').val(code).attr('placeholder', code);
} else if (attempts < 5) {
attempts++;
tryGenerate();
} else {
alert("Unable to generate a unique code. Try again.");
}
});
}
tryGenerate();
}
$(document).ready(function() {
generateAndSetUniqueCode();
});
</script>