Currently, I have logic where if the card number does not satisfy the RegExp then an error is thrown "Number is invalid"
However, I need to change my logic for a special case where if a VI number is entered that fails the RegExp but passes the binExp then DO NOT throw error all while still capturing error for other cards where it throws error if RegExp fails on them.
I had the following logic:
if (cardType != "VI" && (binExp && (binExp.test(cardNumber))) ) { if (RegExp && (!RegExp.test(cardNumber))) throw "Number is invalid"; }
but this won't work for an invalid MC. I want it throw error.
Example, I expect
5204730000001003 to pass
5704730000001003 to fail //but my logic doesn't throw error here. when it should.
JavaScriptXML
Last Comment
badtz7229
8/22/2022 - Mon
leakim971
don't try to optimize your logic with code, just write the code to optimize your logic :
What do you think about :
if ( cardType == "MC" && binExp && binExp.test(cardNumber) && RegExp && RegExp.test(cardNumber) ) {// good }else if ( cardType == "VI" && binExp && binExp.test(cardNumber) && RegExp && RegExp.test(cardNumber) ) {// good}else throw "Number is invalid";
@:leakim971
Yes, except there could be more than 2 types of cards other than VI & MC. I just didn't add it here for simplicity.
That's why my logic becomes more complex I think.
The only special card which needs the alternate path is VI
In my sample code the RegExp & binExp contain the regular expressions that I obtained from the xml.
I used the cardNumber to validate it against those regular expressions.
But I only see ONE XML variable in your code, , the cardNumber variable, which I guess would be used in the RegExp.test, , But from what you say the binExp.test would be on the value in the -
<binExp>^[608623]{6}</binExp>
which is not the same value that's in -
<RegExp>^4[0-9]{12}(?:[0-9]{3})?$</RegExp>
? ?
If you have different RegExpressions, Have you tested the RegExp to see if thy are compatable?
? ?
badtz7229
ASKER
Yes! Correct. It's because for VI they may enter a cardNumber that does not satisfy RegExp but does satisfy binExp so I don't want an error thrown.
but if other card type is entered then do check if that cardNumber satisfies RegExp If fails then throw error.
I hope that makes sense.
badtz7229
ASKER
Thank u
What u said
"don't try to optimize your logic with code, just write the code to optimize your logic :"
Made sense to me and I was able
To reaolve my issue
What do you think about :
Open in new window
or :Open in new window