We help IT Professionals succeed at work.
Get Started

XJS:  how to throw error if no match found

badtz7229
badtz7229 asked
on
210 Views
Last Modified: 2017-05-07
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.
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE