I am working on a requirement to validate tracking information with the following restrictions:
Format: COPYYYY#####
COP is a fixed prefix of each tracking information
YYYY is a year in which the tracking information was submitted (valid years: 2015-2018)
##### is a randomly generated 5 digit number
Tracking information should only have 3 letter characters (COP prefix)
Tracking information should have exactly 9 numeric characters that follow the first 3 letter characters.
Tracking information should be exactly 12 characters long
Since regex is not great solution to validate number range, I decided to check year later once the format is valid.
COP followed by 9 digits (i.e COPXXXXXXXXX)
The regex below is always returning false, even for correct inputs.
/^COP\d{9}$/
Please suggest corrections to above regex and also share thoughts on validating year range in regex (if it is cleaner approach).
Use regex pattern \bCOP(201[5-8])(\d{5})\b
Test source code (JavaScript):
var re = /\bCOP(201[5-8])(\d{5})\b/g;
var s = 'To track your packages, enter COP201812345 and COP201867890 at www.example.org';
var m;
do {
m = re.exec(s);
if (m) {
var e = document.createElement('div');
e.innerHTML = 'Match: ' + m[0] + ' (Year: ' + m[1] + ', Id: ' + m[2] + ')';
document.getElementById("output").appendChild(e);
}
} while (m);
Test source code (Java):
String s = "To track your packages, enter COP201812345 and COP201867890 at www.example.org";
Matcher m = Pattern.compile("\\bCOP(201[5-8])(\\d{5})\\b").matcher(s);
while (m.find())
System.out.println(
"Match: " + m.group() +
" (Year: " + m.group(1) + ", Id: " + m.group(2) + ")");
}
Output:
Match: COP201812345 (Year: 2018, Id: 12345)
Match: COP201867890 (Year: 2018, Id: 67890)
Test it here (JavaScript) and here (Java).
Use the code below.
String value = "COP201812345";
Pattern p = Pattern.compile("^(COP)(201[5-8])(\\d{5})$");
Matcher m = p.matcher(value);
if (m.find()) {
System.out.println("Tracking number " + value + " is valid");
System.out.println("Tracking prefix: " + m.group(1));
System.out.println("Year between 2015 and 2018 is: " + m.group(2));
System.out.println("Random 5 digit number is: " + m.group(3));
}
else {
System.out.println("No match");
}
Output:
Tracking number COP201812345 is valid
Tracking prefix: COP
Year between 2015 and 2018 is: 2018
Random 5 digit number is: 12345
Related
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.
I just learned about the prompt() command; I know that the prompt() command returns user input in the form of a string. I was messing with the program below, and I typed in Per "Dead" Ohlin for the male name. Why did this work and not cause any problems? "Per "Dead" Ohlin..." should have caused a problem. Does the interpreter automatically fix this by putting an escape character before the quotation marks?
let nameOfTheKiller = prompt("Type in a male name.");
let nameOfTheVictim = prompt("Type in a female name.");
let nameOfDrug = prompt("Type in the name of a drug.");
let nameOfAlchoholicBeverage = prompt("Type in the name of an alchoholic beverage.");
let story = nameOfTheKiller
story += " went to a diner, met "
story += nameOfTheVictim + ", and asked her to hangout."
story += " She said yes, so " + nameOfTheKiller + " took her home. As soon as they arrived to "
story += nameOfTheKiller + " relax-location, " + nameOfTheKiller
story += " pulled out " + nameOfDrug + " and " + nameOfAlchoholicBeverage + ". "
story += nameOfTheKiller + " and " + nameOfTheVictim
story += " started using the party favors and got really high and drunk. The party favors gave "
story += nameOfTheKiller + " auditory halucinations that comanded him to kill "
story += nameOfTheVictim + ", so he did." ;
alert("We are done asking you questions. We are generating a story for you. The story will be finished, shortly.");
document.write(story) ;
prompt is not eval - whatever you pass to it will be interpreted as a string. Typing in
Per "Dead" Ohlin
when this line runs
let nameOfTheKiller = prompt("Type in a male name.");
is like doing
let nameOfTheKiller = `Per "Dead" Ohlin`;
Any characters you include in the string you enter which happen to also be valid string delimiters in Javascript will be interpreted as those literal characters (", ', backtick), rather than as delimiters.
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.
Guys i have a google sheet with column taskName and its type is plain text. If I enter any value like 5 jul under this column it is stored as 5 jul.
I add value in my sheet using a code similar to this:
var name = "5 Jul";
var url = "&taskName=" + encodeURIComponent(name);
var encodedString = encodeReservedCharacters(url);
var htmlVal = "<html><script src='" + url + "?action=addName&groupId=" + groupId + "&prefix=addNameCallback" + "&" + encodedString + "'><'/script><'/html>";
$("#checkscript").html(htmlVal);
function encodeReservedCharacters(queryString) {
var reservedCharWithEquivalentHexCode = {
"(": "%28",
")": "%29",
"/": "%2F",
"*": "%2A",
"'": "%27"
};
queryString = queryString.replace(/[()/*']/g, function(match) {
return reservedCharWithEquivalentHexCode[match];
});
return queryString;
};
groupId is my worksheet id that i get using OAuth token
Sheet is updated but instead of "5 Jul", 7/5/2015 is stored. How to solve this?
I had to prepend a tick (') to the cell contents that were suddenly interpreted as date by the new Google Sheets, such as "2015-06" (now I insert "'2015-06")
I'm trying to monospace bibleverse references so that single digit chapters or verses have a leading space.
So "4:5" becomes " 4: 5" and "3:21" becomes " 3:21".
I'm really having problems writing the regex, please help.
I've tried many variations but they essentially boil down to (^\d|\d$), (^\d{1}|\d{1}$) and (^[^0-9]\d|[^0-9]\d$) and many combinations between them
inRef = inChapter + ':' + inVerse;
var inReg = /(^[0-9]{1}|[^0-9][0-9]{1}$)/g;
inRef = inRef.replace(inReg," $1");
console.log(inRef);
Alot of the results I'm getting from my efforts turn references like "6:15" into " 6: 1 5" or " 6:1 5"
Thank you in advance.
Why a regex at all? You've already got the chapter/verse as separate data BEFORE you combined them into the x:y format, so do the formatting there while they're still seperate strings:
if (inChapter.length == 1) { inChapter = ' ' + inChapter }
inRef = inChapter + ':' + inVerse;
Using a regex for this uber-simplistic transformation is akin to nuking a city to get some dust off a shelf instead of using a feather duster.
Given the strings inChapter and inVerse, you could do something like this:
inRef = (" " + inChapter).slice(-2) + ":" + (" " + inVerse).slice(-2)
Note there are two spaces there " " and I'm assuming inChapter and inVerse are only ever 1 or 2 digits.
Edit: Since you need three digits and I assume you still want these to line up, you could do this:
var pad = " "; // this is now THREE spaces!
inRef = (pad + inChapter).slice(-pad.length) + ":" + (pad + inVerse).slice(-pad.length)
So now if you run all your inChapter and inVerse pairs through this, you should get strings that line up like this:
100:100
20:100
2:100
100: 10
100: 1
10: 10
10: 1
1: 1