Avatar of badtz7229
badtz7229
Flag for United States of America asked on

XJS: how to throw error if no match found

I have the following xml file that I am reading via xslt
<root>
      <test>
            <card>VI</card>
            <data>
                  <RegExp>^4[0-9]{12}(?:[0-9]{3})?$</RegExp>
                  <binExp>^[608623]{6}</binExp>
            </data>
      </test>
      <test>
            <card>MC</card>
            <data>
                  <RegExp>(?:^5[1-5]|^2[2-7])[0-9]{14}$</RegExp>
                  <binExp>^[542446]{6}</binExp>
            </data>
      </test>
</root>

Open in new window


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";                        
      
}

Open in new window

           
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

Avatar of undefined
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";        

Open in new window

or :
var testMC = cardType == "MC" && binExp && binExp.test(cardNumber) && RegExp && RegExp.test(cardNumber);
var testVI = cardType == "VI" && binExp && binExp.test(cardNumber) && RegExp && RegExp.test(cardNumber);
if ( !testMC && !testVI ) throw "Number is invalid";

Open in new window

badtz7229

ASKER
@: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.
ASKER CERTIFIED SOLUTION
Member_2_248744

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
badtz7229

ASKER
@Slick812- ur correct.

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Member_2_248744

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
badtz7229

ASKER
I think I gave points to wrong person. I'd like to split the points, if someone could assist?