Link to home
Create AccountLog in
Avatar of tesmc
tesmcFlag 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.
Avatar of leakim971
leakim971
Flag of Guadeloupe image

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

Avatar of tesmc

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
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of tesmc

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.
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?

? ?
Avatar of tesmc

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.
Avatar of tesmc

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
Avatar of tesmc

ASKER

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