Monday 7 January 2013

Cognos TextBox Prompt - Numeric Validation

To validate the value given in Text Box Prompt and allow numeric value alone and text  length should be greater than 10.
 
1.       Create Html Item and place it at the top (Refer Figure 1).
2.       Double click the Html Item and include the below code.

<script>

var fW=(typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]);

function validatePrompt()

{
         str=fW._textEditBoxsPrompt.value;
  if ((str==null||str=="" ))   
                {
                // Show Error Message ...
                alert("Check your value");
                }
              else
                {
                        if ((str.length > 10) &&
                             str.match(/^[-]{0,1}\d*[.]{0,1}\d*$/g))
                       {  return false; }
                        else
                       {
                             if    (((str.length > 0) &&
                                      str.match(/^[-]{0,1}\d*[.]{0,1}\d*$/g)))
                                     { alert('Check your value'); return true;}

                else
                                {alert("Check your value");}
                           }
                    }
            }

function validateTextPrompt()
          {
                str=fW._textEditBoxsPrompt.value;
                if ((str==null||str=="" ))   
                {
                                // Show Error Message ...
                              alert("Check your value");
                }
                else
                {
                                if ((str.length > 10) &&
                              str.match(/^[-]{0,1}\d*[.]{0,1}\d*$/g))
                                {return false; }
                                else
                                {
                                if    (((str.length > 0) &&
                                     str.match(/^[-]{0,1}\d*[.]{0,1}\d*$/g)))                                      {
                                       canSubmitPrompt();
                                       promptButtonFinish();
                                       return true;
                                      }
                                else
                                                {alert("Check your value");}
                                }
                 }             
}
</script>

Figure 1
3.       Create Text Box Prompt and name it as sPrompt.
4.       Create HTML item and place it after the Text Box Prompt.
5.       Double click on Html Item and paste the below code.

<script>

 fW._textEditBoxsPrompt.onblur=validatePrompt;

</script>
6.        With the above code “validatePrompt” function is attached with the “Onblur” event. And this will be triggered at the time of leaving from Text Box Prompt. “ValidatePrompt” function will be called whenever user left from the Text Box prompt.
7.       Without entering any value in the text box prompt the user can click on the “Ok/Finish” button directly. In this case “onblur” event will not be triggered.
8.       To validate this, a HTML Item is placed at the Report footer.  And write the below code inside that HTML item.
 
<input type="BUTTON" class="clsPromptButton" onClick="validateTextPrompt();"  value="OK" 

style="background-color:FFFFFF ;

 font-size:13px;

font-family:verdana,tahoma;

font-weight:Bold,italic;

   color:#000000;

   width:55px;

   height:24px;

border-style:solid;

border-right-color:000000;

border-bottom-color:000000;

border-top-color:CCCCCC;

border-left-color:CCCCCC

"/>
9.       The above code will trigger the “ValidateTextPrompt” function to validate the text box. This function will open the report only after successful validation. Otherwise it will through error message and stay on the same page.
10.   Remove unnecessary prompt buttons and keep the required buttons. Refer Figure 2.

Figure 2

Hope the above example can help you to validate the value and it's length in Textbox prompt.

 

 

 

No comments:

Post a Comment