1

I have a series of reference codes that my end users create during the course of the day. These reference codes correspond to a transaction code that is stored in a database. As of now, there are 15 difference transaction codes.

Here's an example:

Tranasction Code - Reference Code
BB01             - 48912388
C949             - X717-9999

and so on...

New transaction codes can be added at any time. Whether the transaction code is new or not, all corresponding reference codes must be in a certain format. I plan on using regular expressions to validate the reference codes using Javascript.

Now here's the question.

  1. Am I better off doing something like this

    var transactionCode = $('input[name=TransactionCode]').val();
    var referenceCode = $('input[name=ReferenceCode]').val();
    
    // Do this type of test for all 15 types of reference codes until I
    // succeed
    if (transactionCode === 'BB01') {
        var results = /\d{8}/.test(referenceCode);
        // if results is true, blah blah blah
    }
    
  2. Create a SQL table of reference codes and matching regexs. Query the database and construct a javascript object (similar to a dictionary/hash of key value pairs) when needed where I pass the reference code as a key to retrieve the regex to validate against my transaction code.

The second approach seems like overkill for a small data set. But the advantage is I don't have to hard-code my transaction codes when testing and when more codes are added or removed, I don't have to manually mess with them in JavaScript. Where as if I take the first approach, I have to hard code my transaction codes. Not only that, but if the transaction code ever changes, then I have to change the javascript.

I wanted to get someone's feedback on this approach or another alternative if I'm missing something.

2
  • 2
    Questions: (A) Do you mean that transaction-codes (and associated patterns) must be add-able at runtime? (B) Can someone change an existing pattern? (C) So, what happens to data already-entered that might no longer match? (D) How complex can these patterns be? Is it possible for someone to want constraints you cannot satisfy with a regex?
    – Darien
    Commented Apr 28, 2016 at 23:25
  • Have you considered another way to define (store) the tuple code-regex? May be via properties? XML? Are these tuples going to change often? If not, this would be an intermediate way. However you miss the flexibility that DB offers like to manage these tuples with no need to redeploy your server-side application.
    – Laiv
    Commented Jun 1, 2016 at 6:32

2 Answers 2

1

If you load the regex's from database you get a lot more dynamic solution, which I tend to like.

If you don't want to create the complexity right now though, you could start with a few hardcoded patterns and then refactor into a db-driven solution.

I get a bit worried about your validation in javascript though. Client side validation is only for quick user feedback. Final validation must be done server side before information is persisted. It is all too easy to bypass javascript if you want to.

1
  • You are correct about client-side validation. I didn't mention it, but I do server side validation before I submit the data.
    – coson
    Commented Apr 28, 2016 at 22:41
-1

In this case i would

  • store the hashmap of the codes and expressions on the server in cache, where you are already using them for other validation.

  • add a cache expiration to that web call and then in the client side just use the server hashmap result in a for loop for the quick client validation.

  • return the whole hashmap set and store them for some x amount of time on the client. This way the checks stay in sync automatically and changes only need to be made in one place.

If things start growing or getting more complex in the rules around the codes and expressions that would be a candidate at that point to move to a db of some sorts

1
  • this post is rather hard to read (wall of text). Would you mind editing it into a better shape?
    – gnat
    Commented May 29, 2016 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.