Wednesday, September 2, 2015

Working with Reg Ex in IBM BPM

In this post, let us look at some of the commonly used server side data validations using the regular expressions and how to use them with the BPM variables. I am providing ready use methods for some of the commonly used Reg Ex validations in this post just for your reference for future use when ever required.

Reg.Ex for Email Id 

/*  This method will verify if the input string is a valid email Id or not and return true for valid email and false for invalid email */ 
function isEmail(str){
     if(str!=null && str!=""){
        var emailRegEx=/^\w+[\w-\.]*\@\w+((-\w+)|(\w*))\.[a-z]{2,3}$/g;
         return str.match(emailRegEx);
     }else{
         return false;
     }
}


Reg.Ex for URL

/*  This method will verify if the input string is a valid url or not and return true for valid url and false for invalid url */
function isGoodUrl(str) {
    var regEx=/(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi;
    if(str!=null && str!=""){       
                    return str.match(regEx);
        }else{
                   return false;
    }
}

Reg.Ex for trimming of trailing spaces

/*  This method will verify if the input string is a valid email Id or not and return true for valid email and false for invalid email */
function trim(str){
       if(str!=null){
                 return str.replace(/^\s+|\s+$/, '');
      }else{
                 return "";
     }
}

Having defined these methods in the either js file which is added as server file in File section in PD and can be used globally across the application or these can be defined locally in a server script and can be invoked at that server script level as below.

For trim:  tw.local.str = trim(tw.local.str);
For URL: tw.local.isURL= isGoodUrl(tw.local.url)
For Email : tw.local.isEmail= isEmail(tw.local.emailId)
  
Hope this post is helpful !!



3 comments:

  1. Very helpful! Thanks!

    ReplyDelete
  2. Have you ever tried to use RegExp in loops. In BPM 8.5.6 they have a bug. The first iteration it works perfectly, but after that it returns always null. So, take care :-)

    ReplyDelete
  3. Hi Nick,
    I have never used regex in loops. But yes I will keep in mind. Thanks for pointing.

    ReplyDelete