Masking credit card number if a CC number entered - javascript

var cardNumber = '4761640026883566';
var cardNumberDashed = '4761-6400-1234-2345';
var cardNumberSpaced = '4761 6400 1234 3523';
var ensureOnlyNumbers = R.replace(/[^0-9]+/g, '');
var maskAllButLastFour = R.replace(/[0-9](?=([0-9]{4}))/g, '*');
var hashedCardNumber = R.compose(maskAllButLastFour, ensureOnlyNumbers);
document.body.innerHTML = hashedCardNumber(cardNumber) + '<br/>' +
hashedCardNumber(cardNumberDashed) + '<br/>' +
hashedCardNumber(cardNumberSpaced);
My situation is a bit complicated, I have a <textarea> that I use as a message field in a chat window. I want to mask all credit card numbers that was sent on this chat, but NOT every number, because I do need a membership numbers from clients which is 10-15 digits numbers.
<textarea id="postMessage"></textarea>
I followed the code in this jsfiddle: http://jsfiddle.net/7odv6kfk/ but it works only on input fields that have credit card numbers.
How can I do that?
Thank you!

http://jquerycreditcardvalidator.com/
this library may help you it can detects only credit card number and avoid other numbers
var result = $('#cardnumber').validateCreditCard({ accept: ['visa', 'mastercard'] })

Here you can find valid CC number formats and regular expressions matching them:
http://www.regular-expressions.info/creditcard.html
The problem is that old VISA cards have 13 digits, American Express have 15 digits and some JCB cards have 15 digits. This conflicts with your membership numbers. Do you need to support 16-digit CC numbers then?
The following code shows how to replace 16-digit CC numbers with asterisks:
var re = /\b(\d[ -]?){15}\d\b/g;
var text = "My CC number is 4761640026883566 (or 4761-6400-1234-2345, or 4761 6400 1234 3523). My membership number is 1234567890123."
document.body.innerHTML = text.replace(re, "****");
Output:
My CC number is **** (or ****, or ****). My membership number is 1234567890123.

Related

I must avoid entering letters on my digipas

I have a small problem. When I enter a new transfert of 269 euros with the account bank number BE072750044-35066. I have to introduce a code of confirmation. The code is 350269.
The figures 350 are the last 5 figures of the bank account number -> BE072750044-35066.
And the 269 represents the amount for the new transfert.
Another example, if the new transfert was of 350 euros. We will have 350350.
Now my problem, if I have an account bank with a letter at the end. FR5940802053780006178110K61.
The problem is that I retrieve 10K269, but the user must enter only numbers on the digipas.
How to avoid this problem for the user, please?
getTokenTwoAdd(nt) {
var partOne, partTwo, absoluteAmount;
partOne = (nt.iban.substr(nt.iban.length - 5)).substring(0, 3);
absoluteAmount = Math.abs(nt.amount);
absoluteAmount = parseInt(absoluteAmount);
partTwo = ((absoluteAmount < 100) ? this.helpers.addLeadingZeroesLeft(absoluteAmount, 3) : absoluteAmount) + "";
partTwo = partTwo.substring(0, 3);
console.log("Iban number, slice => " + partOne);
console.log("Iban number => " + nt.iban);
console.log("Amount => " + absoluteAmount);
return partOne + partTwo;
}
The variable partOne represents the account bank number with the slice
The variable nt.iban is the accout bank number
The variable absoluteAmount is the amount for the new transfert
To get rid of any characters that are not digits, you could use
partOne = nt.iban.replace(/\D+/g, '').substr(-5, 3);
where \D+ matches one or more non-digit characters.

VueJS: Computed Calculation Assistance

I need to be able to convert a string (IP address) such as this 10.120.0.1 to a string (ISIS Network ID) such as this 49.0001.0101.2000.0001.00. The middle section 010 1.20 00.0 001 corresponds to the first string (I've spaced them out to show the IP address is inside it). You can see that there are 4 digits in each ISIS Network ID hextet that need to correspond to 3 digits in the IP Address octet. A number of 53 for example would have a leading 0 to make 3 digits.
All the IP addresses start with 10.120. so I just need to inject the last 2 octets from the IP Address into the ISIS Network ID.
I need this to be dynamic so when someone types in another ip address into a loopbackIP input, it automatically updates the isisNetworkID field.
I have this:
49.0001.0101.{{ isisNetworkID }}.00
This needs to take the value from an input v-model="loopbackIP" that I have and translate the remaining values to sit in the middle of that isisNetworkID following this format - xxxx.xxxx.
I've got this computed calculation but I'm not sure how to make 4 digits equal 3...
const loopbackIP = '10.120.0.1';
const isisNetworkID = computed(() => {
let idaho = '10.120.';
if (loopbackIP.indexOf(idaho)) {
return loopbackIP.slice(7);
} else {
console.log('Nothing is happening');
}
});
I hope this makes sense...
I think I understand what you're trying to achieve. Let's break it down into digestible parts. You have an IP address of:
10.120.0.1
And you want to transform it such that each part is padded to 3 digits:
['010', '120', '000', '001']
This can be done by splitting the string by the . character, and the using String.prototype.padStart(). We then join the array back into a string:
'010120000001'
||||
^^^^ -> to be deleted
We know that the first 4 digits is not needed, since it's already part of your template, so we can remove them using String.prototype.substring(4). That leaves us with:
'20000001'
Now it is just the matter of splitting it into 4 characters per item:
['2000', '0001']
...and rejoining it with . character:
'2000.0001'
...and interpolating it back into the string. I have a proof-of-concept example below, which should output the desired string:
const loopbackIP = '10.120.0.1';
const parts = loopbackIP.split('.').map(x => x.padStart(3, '0'));
// Remove the first 4 characters
let isisNetworkId = parts.join('');
isisNetworkId = isisNetworkId.substring(4);
const output = `49.0001.0101.${isisNetworkId.match(/.{4}/g).join('.')}.00`;
console.log(output);
So if you want to translate it to your VueJS code, it should look no different that this:
const loopbackIP = '10.120.0.1';
const isisNetworkID = computed(() => {
const loopbackIP = '10.120.0.1';
const parts = loopbackIP.split('.').map(x => x.padStart(3, '0'));
let isisNetworkId = parts.join('');
isisNetworkId = isisNetworkId.substring(4);
// Rejoin, split into items of 4-character long, rejoin by period
return isisNetworkId.match(/.{4}/g).join('.');
});

Building a Custom RNG

I am building an app for people who like to play the lottery. The app will take in data from the user such as birthdays, street addresses, license plates, lucky numbers, times, etc. The user will fill their number diary with whatever they want. I will concatenate these strings of numbers into a solo string for each user.
I then will run the following function to get the frequency of each digit 0 - 9 as it appears in the string.
var digitFreq= function(numString){
zeros = numString.split("0").length-1
ones = numString.split("1").length-1
twos = numString.split("2").length-1
threes = numString.split("3").length-1
fours = numString.split("4").length-1
fives = numString.split("5").length-1
sixes = numString.split("6").length-1
sevens = numString.split("7").length-1
eights = numString.split("8").length-1
nines = numString.split("9").length-1
numbers = numString.length;
zeroPrcnt = zeros/numbers
onePrcnt = ones/numbers
twoPrcnt = twos/numbers
threePrcnt = threes/numbers
fourPrcnt = fours/numbers
fivePrcnt = fives/numbers
sixPrcnt = sixes/numbers
sevPrcnt = sevens/numbers
eightPrcnt = eights/numbers
ninePrcnt = nines/numbers
}
This will return the frequency % of each digit in the number string. My question is how do I take these frequencies and dynamically build an RNG that uses each user's personal frequency percentages when drawing a three digit, four digit, or mega lottery number?
The user will use their RNG to generate lottery numbers to play.
You could just concatenate all entries in one string and then randomly choose a character from that string. Each number would have a probability related to the number of times it appears in the string.

Trying to understand how to use parseFloat to convert string to number

I'm trying to understand how to user parseFloat to convert a string to a number--which by the way changes based on a users rewards club points.
Here's the code I've written so far:
var ptsBalance = jQuery('.value.large.withCommas').text(); // get current RC points balance
var strParse = parseInt(ptsBalance, 10); // * Output should be current RC balance
alert(strParse); // alert Rewards Club Balance
var bonusPoints = 70000;
var totalPts = jQuery(strParse + bonusPoints); // sum total of Bonus and Current points
jQuery(strParse).appendTo('.currentPts');
jQuery(totalPts).appendTo('.totalPts'); // insert RC points balance after 'Current Points' text
Clearly I'm not using pareFloat, but rather strParse, which is rounding my string. That said, how do I convert a string to a number that could be "10", "100", "1,000", "10,000" etc.?
Here's a link to my Fiddle: http://jsfiddle.net/bkmills1/60vdfykq/
Still learning...
Thanks in advance!
In your fiddle, change line 4 from:
var strParse = parseInt(ptsBalance, 10);
to
var strParse = parseInt(ptsBalance.replace(',',''), 10);
You may also want to remove any other symbols needed, or even use regular expressions to do just that.

Regex not working properly, returns null when it shouldnt

I know there are a million regex questions out there, and I assure you I have read a million -1 of those! This is just one of those things that are not for me. I am trying to extract the price number (decimal or not) and the name of the item (everything between the price number and < br/>)
Input:
var content = "$22 M Uniform <br/>"
or something like this
var content = "$39 Day / Evening Class Tuition <br/>"
or
var content = "$12.8 Weekend Class Payment <br/>"
my code :
var itemprice = content.match(/(\d+)(d*(\.\d{1,2}))/);
var itemname = content.match(/(\s)(.*?)</);
My Issue:
My itemname works as it should and returns properly however my number seeker seems to not work in the browser when I debug it itemprice is set to null. This is weird because when I try that code here http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_regexp_i it works just fine, extracting the number.
For the website my code looks like:
var str = "$39.56 Day / Evening Class Tuition <br>";
var patt1 =/(\d+)(d*(\.\d{1,2}))/;
var matches = str.match(patt1)
alert(matches[0]);
A simple decimal regex with optional points is
/\d+(\.\d{1,2})?/
Demo here - http://jsfiddle.net/JXdbg/2/
For dollar-sign currency detection, I would prefix this with the $
/\$\d+(\.\d{1,2})?/

Categories