ssn masking

  • 1
  • Problem
  • Updated 4 years ago
  • Solved
Hi,

Suppose if any body enter ssn as 123456789  i need to modify the field value as 123-45-6789.
I have written below java script code       snippet. That is not working.


(function(skuid)
{
var $ = skuid.$;
$(function(){

    var myModel = skuid.model.getModel('Offender');
    var row = myModel.getFirstRow();
    var params = arguments[0];
  
    var field = arguments[0];

    var ssn = arguments[1];

var firstthree=ssn.substr(1,3);
var midtwo=ssn.substring(4,5);
var lastfour=ssn.substring(6,9);
var text= firstthree + "-" + midtwo + "-" + lastfour;
skuid.ui.fieldRenderers.TEXT[field.mode](field, text);
});

})(skuid);

Is there a way to debug the code.
Photo of Arjun Arjun

Arjun Arjun

  • 230 Points 100 badge 2x thumb

Posted 4 years ago

  • 1
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
A few things that others here in the community have taught me.
  1. Use the console in the browser. In Chrome, you can open it by hotkey Ctrl + Shift + J.
  2. Use the console.log in your javascript to see what is the value of what you put in the log. ie. console.log(firstthree)
  3. Open the Source tab once in the console. In there you will find the code you've written. You can set breakpoints and watched expressions to see what is going on with the code. There are plenty of tutorials on this on youtube. Search "debug javascript in Chrome".
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
You want you're snippet to be of type "Inline snippet" not "Inline". It should also be linked to the SSN field that you're trying to render differently. This should be all you need:
var field = arguments[0];
var ssn = arguments[1];
var firstthree=ssn.substring(1,3);
var midtwo=ssn.substring(4,5);
var lastfour=ssn.substring(6,9);
var text= firstthree + "-" + midtwo + "-" + lastfour;
skuid.ui.fieldRenderers.TEXT[field.mode](field, text);
(Edited)
Photo of Arjun Arjun

Arjun Arjun

  • 230 Points 100 badge 2x thumb
Hi Moshe,

As you told i have tried below code

(function(skuid)
{
var $ = skuid.$;
$(function(){
    var params = arguments[0];
    var ssn = arguments[1];
    var firstthree=ssn.substring(1,3);
var midtwo=ssn.substring(4,5);
var lastfour=ssn.substring(6,9);
var text= firstthree + "-" + midtwo + "-" + lastfour;
skuid.ui.fieldRenderers.TEXT[field.mode](field, text);
});

})(skuid);



i am getting error  as  cannot read property 'substring' of undefined. I believe SSN value is not copying from arguments[1]. What could be the error
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
You have your code wrapped in an anonymous function, so you're not getting any arguments in your function. You should write it as I did above, you need to remove this part :
(function(skuid){
...
})(skuid);
Photo of Arjun Arjun

Arjun Arjun

  • 230 Points 100 badge 2x thumb
Jim:   Yes i changed Resource Location to 'Inline Snippet'.
Photo of Arjun Arjun

Arjun Arjun

  • 230 Points 100 badge 2x thumb
Moshe,

Sorry, i have attached previous error screenshot, here is the actual screen shot for what i am talking. Could you please look at watch expressions window.

Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
OK I believe I said this a couple of times already, you have to get rid of the
$(function
line, you had it right before. If you just replace your whole snippet with this:

var field = arguments[0];
var ssn = arguments[1];
var firstthree=ssn.substring(0,3);
var midtwo=ssn.substring(3,5);
var lastfour=ssn.substring(6,10);
var text= firstthree + "-" + midtwo + "-" + lastfour;
skuid.ui.fieldRenderers.TEXT[field.mode](field, text);
It will work!
Photo of Arjun Arjun

Arjun Arjun

  • 230 Points 100 badge 2x thumb
Moshe,

As you suggested i have removed 

$(function

and tried with the below code. It didn't work. 

var $ = skuid.$;
var params = arguments[0];
var ssn = arguments[1];
var firstthree=ssn.substring(1,3);
var midtwo=ssn.substring(4,5);
var lastfour=ssn.substring(6,9);
var text= firstthree + "-" + midtwo + "-" + lastfour;
skuid.ui.fieldRenderers.TEXT[field.mode](field, text);

The error is in the below line

var params = arguments[0]; // error
var field = arguments[0]; // correct

we have to get arguments[0] to field, not to params. This has solved my problem.
Thank you so much for your help.
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Yup.  The field in the fieldRenderers line would be undefined if the variable you defined above was named "params"   Glad you got this working.   And than you Jim and Moshe for your help with this question...