If you are unable to create a new account, please email support@bspsoftware.com

 

News:

MetaManager - Administrative Tools for IBM Cognos
Pricing starting at $2,100
Download Now    Learn More

Main Menu

How to ignore an optional preceding space in REGEXP string [SOLVED]

Started by hanfrie, 29 Apr 2016 08:36:50 AM

Previous topic - Next topic

hanfrie

I'm trying to build a javascript that checks the entered values in a text prompt field "Initials" for the existance of the obligatory dot after each letter. So, for instance

A.B. is OK
AB   is NOT OK
A.B  is NOT OK
etc.

I found the standard Cognos code example for verifying a postalcode (Samples Prompt API, (013) - Validate Type-In Postal Code Values), and managed to alter that in such a way that it does the trick. But I can't get it to work when the user just clicks on the prompt and starts to enter the initials, even together with the dot directly following the initial, because of the fact, that the text prompt has a default value of a space. So when the user clicks on this Text Box prompt and directly starts entering initials, the standard space will remain there on position one. So you get

A. is NOT OK (because of the preceding space)

So the user now must explicitly select the default space first and overwrite this with an initial. And I don't like that!

Obviously I could remove the default space in the definition of the text prompt, but that is not an option because the resulting parameter is considered further down the line as an obligated parameter value, so one way or another I need to deal with that optional preceding space.
Based on what I found googling the internet, I should be able to use the \S parameter in the REGEXP function that I use to validate the field, but I cannot get it to work, and ask you all for help in how to solve this issue.

Below you find the current code I use to validate the initial field. The code is in a HTML item positioned directly after the Text Box prompt (named "Prmptvrltrs1").

Hope to hear from you

hanfrie


<script>
/*
*
* Attach functions to an arbitrarily named object to mimic a namespace to ensure the name uniqueness
*
*/
var asdf = { };

/*
*
* This function will associate a validation function with the prompt control called Prmptvrltrs1
*
*/
asdf.assignValidator = function ( ) {
// Create the report object
oCR = cognos.Report.getReport( "_THIS_" );

// Create an array of prompt controls
// This object is used in both functions in this script.
asdf.promptControlCode = oCR.prompt.getControlByName("Prmptvrltrs1");

// Make sure we have a valid Code
asdf.promptControlCode.setValidator(asdf.CodeValidator);

};


/*
*
* This function is called for each character entered by the user.
* Ensure the value is a valid Code (ie. a.b. ) using RegExp.
*
*/

asdf.CodeValidator = function(promptValue) {
//promptValue is an array of 1 value

// If a value has been entered by the user ensure it matches the pattern
if (promptValue.length >0) {
var reCodeFormat = new RegExp("^([A-Z][/.])+$", "gi" );
if ( reCodeFormat.test(promptValue[0].use) ) {
return true;
} else {
    return false;
}
} else {
// If the prompt contains no value it is valid.
// This is important as the prompt and filter are optional
return true;
}
};



/*
*
* Execute the function to assign the validation function to the prompt control
*
*/
asdf.assignValidator( );

</script>





bdbits

I would think you should be able to resolve using the prompt "further down the line" without needing the space. Use an expression to check if the parameter is missing at that point in time and substitute a value accordingly.

hanfrie

That's what I thought too.... I tried and failed.  :(

This prompt itself is not required. The value is picked up in a calculated query item as

upper(trim(both,(if (?cpk_vltrs_contact?='' or ?cpk_vltrs_contact? = NULL) then (' ') else (?cpk_vltrs_contact?))))

and displayed on the report page as a Singleton. But, the parameter is obligated, and I can't figure out why.

hanfrie

I finally managed to solve the issue by changing the code. See below. I added a test on the value of the first character.

<script>
                /*
                *
                * Attach functions to an arbitrarily named object to mimic a namespace to ensure the name uniqueness
                */
                var asdf = { };
                /*
                *
                * This function will associate a validation function with the prompt control called Prmptvrltrs1
                *
                */
                oCR = cognos.Report.getReport( "_THIS_" );

                // Create an array of prompt controls
                // This object is used in both functions in this script.
                asdf.promptControlCode = oCR.prompt.getControlByName("Prmptvrltrs1");
                asdf.promptControlCode.setValidator(
function (values) {
var result = false;
                                           if (values.length == 0) { result = true };
                                           if (values.length == 1 && values[0]['use'] == " ") { result = true };
if (values.length > 0) {
var sValue = values[0]['use'];
var reCodeFormat = new RegExp("^([A-Z][/.])+$", "gi" );
if ( reCodeFormat.test(sValue ) ) {
result=true;
}
}
return result;
}
);

</script>