You need to sign in to do that
Don't have an account?
Ramk123
SSN formatting in Lightning page
I have a requirement to automate SSN structure.
example:
user input SSN: 123456789
automate output SSN format: 123-45-6789
with my below logic am not able to achieve the scenario, anyone please help.
Component:
<lightning:input aura:id="SSN__c" name="SSN"
label="SSN"
maxlength="9" onblur="{!c.formatSSN}"
value="{!v.newCallerModel.Ssn}" />
JS Controller
formatSSN: function(component, helper, event) {
var patt = new RegExp("\d{3}[\-]\d{2}[\-]\d{4}");
var x = document.getElementById("SSN__c");
var res = patt.test(x.value);
if(!res){
x.value = x.value
.match(/\d*/g).join('')
.match(/(\d{0,3})(\d{0,2})(\d{0,4})/).slice(1).join('-')
.replace(/-*$/g, '');
}
},
example:
user input SSN: 123456789
automate output SSN format: 123-45-6789
with my below logic am not able to achieve the scenario, anyone please help.
Component:
<lightning:input aura:id="SSN__c" name="SSN"
label="SSN"
maxlength="9" onblur="{!c.formatSSN}"
value="{!v.newCallerModel.Ssn}" />
JS Controller
formatSSN: function(component, helper, event) {
var patt = new RegExp("\d{3}[\-]\d{2}[\-]\d{4}");
var x = document.getElementById("SSN__c");
var res = patt.test(x.value);
if(!res){
x.value = x.value
.match(/\d*/g).join('')
.match(/(\d{0,3})(\d{0,2})(\d{0,4})/).slice(1).join('-')
.replace(/-*$/g, '');
}
},
All Answers
Here is the code you can use ...
Controller code
Thanks for your response!
The above logic working fine, but my expected SSN format is 123-45-6789 with the input 123456789.
Appreciate your response in advance.