is there any way to mask the output of the input field once passed to the next page
Enter Card : 123456789011
after the data has been passed onto the next page
when displayed it should look like this
Card info: ********9011
the first 8 digits was converted to asterisk and
the last 4 digits of the card is visible.
If you've already ensured that the card number is a valid length:
card = "123456789011";
output = "********" + card.substring(card.length-4);
is all you'll need. Output will now be as you wanted, and although Starx's answer is dynamic: it's also overkill.
Something like this
var card = "123456789011";
var str = "";
//Create the "*" for exactly 4 digits short
for(var i=1; i <= card.length-4; i++) {
str += "*";
}
//Join the asterisk and last 4 characters
ecard = str + card.substr(card.length-4);
console.log(ecard);
Demo
Related
I am new in replacing number into some characters in specific places. I have this set of number 123-45-6789 but I need to show only like this XXX-XX-6789. But I need to change only the number not including the "dash".
Below is my sample code
var mainStr = $("#view_ssn").text(),
vis = mainStr.slice(-4),
countNum = '';
for(var i = (mainStr.length)-4; i>0; i--){
countNum += 'X';
}
$("#view_ssn").text(countNum+vis);
Output
XXXXXXX6789
You may simply use:
var ssn = "123-45-6789";
var output = ssn.replace(/\d(?=.{5,})/g, "X");
console.log(output);
The logic behind the regex pattern \d(?=.{5,}) is to replace every digit in the input Social Security number which has at least 5 characters in front of it. This excludes the last 4 digits of the SSN.
Another option could be to match the exact allowed pattern and capture the part that you want to keep in group 1.
In the replacement use XXX-XX and concatenate it with the group 1 value.
const regex = /^\d{3}-\d{2}(-\d{4}$)/;
const str = "123-45-6789";
console.log(str.replace(regex, (m, g1) => "XXX-XX" + g1));
I am using extendscript to build some invoices from downloaded plaintext emails (.txt)
At points in the file there are lines of text that look like "Order Number: 123456" and then the line ends. I have a script made from parts I found on this site that finds the end of "Order Number:" in order to get a starting position of a substring. I want to use where the return key was hit to go to the next line as the second index number to finish the substring. To do this, I have another piece of script from the helpful people of this site that makes an array out of the indexes of every instance of a character. I will then use whichever array object is a higher number than the first number for the substring.
It's a bit convoluted, but I'm not great with Javascript yet, and if there is an easier way, I don't know it.
What is the character I need to use to emulate a return key in a txt file in javascript for extendscript for indesign?
Thank you.
I have tried things like \n and \r\n and ^p both with and without quotes around them but none of those seem to show up in the array when I try them.
//Load Email as String
var b = new File("~/Desktop/Test/email.txt");
b.open('r');
var str = "";
while (!b.eof)
str += b.readln();
b.close();
var orderNumberLocation = str.search("Order Number: ") + 14;
var orderNumber = str.substring(orderNumberLocation, ARRAY NUMBER GOES HERE)
var loc = orderNumberLocation.lineNumber
function indexes(source, find) {
var result = [];
for (i = 0; i < source.length; ++i) {
// If you want to search case insensitive use
// if (source.substring(i, i + find.length).toLowerCase() == find) {
if (source.substring(i, i + find.length) == find) {
result.push(i);
}
}
alert(result)
}
indexes(str, NEW PARAGRAPH CHARACTER GOES HERE)
I want all my line breaks to show up as an array of indexes in the variable "result".
Edit: My method of importing stripped all line breaks from the document. Using the code below instead works better. Now \n works.
var file = File("~/Desktop/Test/email.txt", "utf-8");
file.open("r");
var str = file.read();
file.close();
You need to use Regular Expressions. Depending on the fields do you need to search, you'l need to tweek the regular expressions, but I can give you a point. If the fields on the email are separated by new lines, something like that will work:
var str; //your string
var fields = {}
var lookFor = /(Order Number:|Adress:).*?\n/g;
str.replace(lookFor, function(match){
var order = match.split(':');
var field = order[0].replace(/\s/g, '');//remove all spaces
var value = order[1];
fields[field]= value;
})
With (Order Number:|Adress:) you are looking for the fields, you can add more fields separated the by the or character | ,inside the parenthessis. The .*?\n operators matches any character till the first break line appears. The g flag indicates that you want to look for all matches. Then you call str.replace, beacause it allows you to perfom a single task on each match. So, if the separator of the field and the value is a colon ':', then you split the match into an array of two values: ['Order number', 12345], and then, store that matches into an object. That code wil produce:
fields = {
OrderNumber: 12345,
Adresss: "my fake adress 000"
}
Please try \n and \r
Example: indexes(str, "\r");
If i've understood well, wat you need is to str.split():
function indexes(source, find) {
var order;
var result = [];
var orders = source.split('\n'); //returns an array of strings: ["order: 12345", "order:54321", ...]
for (var i = 0, l = orders.length; i < l; i++)
{
order = orders[i];
if (order.match(/find/) != null){
result.push(i)
}
}
return result;
}
I am trying to construct a statement (because the client requests for it) and want to remove the last statement in a for loop but still included in between the loop.
This is what I am trying to achieve (as a broad example as I can):
I selected: 2 years period, 2 yrs period, 1 years period, 1 yrs period.
I managed to achieve this but at the very end, it doesn't end with a period, it ended with a comma instead.
The number of years is selected by the user, therefore I declared a variable.
Here is my code in Javascript:
var out_content = document.getElementById("content");
var in_year = 2;
var in_period = ["years","yrs"];
var sText = "I selected: ";
for (var i=0;i<in_year; in_year--)
{
for (var p=0;p<in_period.length;p++)
{
sText += in_year + in_period[p];
sText += ",";
}
}
out_content.innerHTML = sText;
What do I do?
The simplest way to solve your problem is to just remove the last character (the unwanted comma) and put a period there instead.
sText.slice(0,-1)+'.'
Slicing from 0 to -1 means getting the characters from the beginning to the one before the last.
Add the if statement before comma and check with the variable value with p and the condition should pass but should fail on last loop statement
Removing the last character from your string, when predictable, can be easily achieved as such:
sText = sText.substring(0, sText.length-2);
Substring takes two parameters, start (0) and end (you want length minus 1 here, but since its viewed as an array you might want length - 2). Heres more info: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/substring
A better way to handle this kind of problem is to use an array, then join array elements:
var sText = "I selected: ";
var aText = [];
for (var i=0;i<in_year; in_year--)
{
for (var p=0;p<in_period.length;p++)
{
aText.push(in_year + in_period[p]);
}
}
sText += aText.join() + '.';
I have the following example where I am putting a limit on the characters entered in the Textarea:
var tlength = $(this).val().length;
$(this).val($(this).val().substring(0, maxchars));
var tlength = $(this).val().length;
remain = maxchars - parseInt(tlength);
$('#remain').text(remain);
where maxchars is the number of characters. How can I change this example to work with words, so instead of restricting chars, I restrict to a number of words.
http://jsfiddle.net/PzESw/106/
I think you need to change one string of your code to something like this:
$(this).val($(this).val().split(' ').slice(0, maxchars).join(' '));
This code splits text in an array of words (you may need another workflow), removes extra words and joins them back
A simple way would be to converting the full String into array of words.
For example you're having a String as:
var words = "Hi, My name is Afzaal Ahmad Zeeshan.";
var arrayOfWords = words.split(" "); // split at a white space
Now, you'll have an array of words. Loop it using
for (i = 0; i < words.length; i++) {
/* write them all, or limit them in the for loop! */
}
This way, you can write the number of words in the document. Instead of characters!
I have a long string.
var string = "This is a long string."
I also have a function to download the string to "path".
downloadfunction{... 'echo "'+string+'" >> '+path;}
How do I execute this function for every 2 letters of the string? Read somewhere about using ".length" but not sure how to implement it in this case. I also do not want to split the string into an array. The download function should help split the string to download 2 letters progressively.
I.e. I would like to download the string 2 letters at a time.
EDIT: To clarify, the string needs to be downloaded x characters at a time as the download would break if it exceeds that limit.
Here is an example that is commented on how to do this:
var string = 'a really really really long string \
that I want to split by two letters at a time';
// this needs to be ceiling so that it always rounds up on odd numbers
// otherwise the last letter may be left out
var iterations = Math.ceil(string.length / 2);
for (var i = 0; i < iterations; i++) {
// we are iterating over half of the length but want to go two letters at a time
var j = i*2;
// make a new string with the first letter
var letters = string[j]
// this is an if statement to check if there is a second letter
// if there is concat it to the first letter
// otherwise the last set of an odd length would concat `undefined` to it
if (string[j+1]) { letters += string[j+1]; }
// call your function here passing `letters` to it
downloadfunction{... 'echo "' + letters + '" >> '+path;}
}
count string length
divide by 2 round up or down what ever you like
then do a for loop of the amount after the devision.
Somethig like this
//Your string that is downloaded
var string = "This is a long string."
//calculate the amount of character in the string
var amount = string.length;
//divide the string by 2
var roundnr = amount/2;
for (var i=0;i<Math.round(roundnr);i++)
{
//do something here for every 2 characters in the string
}