Hi everyone,
I have a Single Line Field, I am trying to achieve dual behaviour using the inline javascript. I want the field to behave as a US Postal Code, if no alphabet characters detected, but if there are characters detected more than 2 than it to act as a Canadian Postal code with and it to mask too. Here is the code I am using.
Would appreciate your help on this. Thank you!
function formatPostalCode(postalCodeInput) {
const cleanedInput = postalCodeInput.replace(/\W/g,'');
if (/^\d+$/.test(cleanedInput === 5)
{
return cleanedInput;
}
else{
return 'Invalid US Code'
}
else{
if (/^[A-Za-z0-9]{6}$/.test(cleanedInput)){
return cleanedInput.substring(0,3)+ ' ' + cleanedInput.substring(0,6);
}else{
return 'Invalid Canadian postal code';
}
}
}
LFForm.onFieldBlur(function(){ LFForm.setFieldValues({fieldId: 261}, formatPostalCode(LFForm.getFieldValues({fieldId: 261}))) }, {fieldId: 261})