No announcement yet.

REGEXP for field validatiopn

  • Filter
  • Time
  • Show
Clear All
new posts

  • REGEXP for field validatiopn

    I have a complex formatted field to validate that I use REGEXP to validate normally.

    FFF= First three letters of the first name (* is used to fill empty spaces)
    I=Middle initial (* if not specified)
    LLLL= First 4 letters of last name (* is used to fill empty spaces)

    example: October 17, 1998, John D Roe - would be 101798JOHDROE*

    example: February 29, 2000, Mike Smith - would be 022900MIK*SMIT


    Being new to scriptcase, how do I implement a REGEXP validation inside scriptcase?

    Last edited by John Coombs; 06-03-2016, 10:49 AM.

  • #2

    $tst = {USSNumber};
    $regex = "^(?!023)(?!0229.[13579])(?!0229[02468][26])(?!0229[13579][048])(?!(0[469]|11)31)(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])([0-9]{2})(?!.?[*][^*])([a-zA-Z*]{3})[a-zA-Z*](?!.{0,2}[*][^*])([a-zA-Z*]{4})$";

    If (!preg_match($regex,$tst) {
    sc_error_message('Please enter the right value');

    I have tried this bu I believe the error is related to the curly brackets [ {} ]!!! How do I specify the { and } in a regular expression to get validation to work!?!?!


    • #3
      Share my pain, been asking similar for ages I figure nobody on the forum knows and it's not possible in this version of SC hence NetMake's silence too.


      • #4
        Hi there!

        It seems like Scriptcase replaces every single {some value} or [some value] whenever it finds, even before our code runs. And I don't see any oficial way to solve that.

        But analysing this case today, I found a workaround. If you put a character (except for letters/numbers) right after the opening character "[" or "{", Scriptcase won't replace it. So you could just put a space character, and then have a function to escape that. And because Scriptcase doesn't parse it recursively, it will stick as it is. Something like this:

        // you can create this function as a lib and use whenever you need
        function escapeScSpecialSymbols($str){
            return str_replace(
                Array("{ ", "[ "),
                Array("{", "["),
        // then just call it, passing the string with the text to escape
        echo escapeScSpecialSymbols("{ 20}[ asd]"); // this will return/echo {20}[asd]
        SC Tools: A browser extension to make working with ScriptCase simpler and even more productive.


        • #5
          Hi Mamende, this is very insightful. For my own scenario ( I don't believe it will work though because the output of the function will still be passed into sc_exec_sql macro.


          • #6
            Hi scriptcaser! I've just tested that regexp using 'escapeScSpecialSymbols' and the macros 'sc_exec_sql' and 'sc_lookup', and in both cases it has worked. Hasn't it worked for you?

            It seems, to me, that ScriptCase only parses our code once at the beginning of the process and then leaves it "as is". So, in theory, this workaround should work for you too...
            SC Tools: A browser extension to make working with ScriptCase simpler and even more productive.



            • #7
              Oh that should be correct: the macros are changed by a preprocessor not the run-time engine. Super! Thanks a lot.


              • #8
                You're welcome!
                SC Tools: A browser extension to make working with ScriptCase simpler and even more productive.



                • #9
                  I found another way to work around this issue, maybe it will be useful to someone.

                  I used libraries functionality where I created code that would have issues getting phrased by scriptcase, then included the library file and called the function, in my code. Everything worked smoothly

                  To do this click on tools, the internal libraries, then create new library if you do not already have one.

                  Create function in new file that will run your code.

                  Inside your application, go to Programming and Internal Libraries or just libraries, the enable library file you added.

                  Now call the function in your code, and it should phrase fine.