- IBM Cognos 8 Report Studio Cookbook
- Abhishek Sanghani
- 393字
- 2021-08-06 17:15:50
Validating textbox prompts
There is a report with a textbox prompt. Users are expected to enter a phone number in (nnn)nnn-nnnn format in that prompt.
In this recipe, we will write a code to validate the value entered by the user and submit the report only if the value entered is in specified format.
Getting ready
Pick any report and add a textbox prompt to it. We will add a JavaScript to validate that textbox.
How to do it...
- Wrap the textbox prompt within a SPAN in the same way as we did in prior recipes.
- Add the following script to the page footer:
<script> function ValidatePage() { var theSpan = document.getElementById("A1"); var a = theSpan.getElementsByTagName("input"); /* this captures the textbox */ for( var i = a.length-1; i >= 0; i-- ) { var link = a[i]; if( link.id.match(/PRMT_TB_/)) {phoneRegex = /^\(\d{3}\) \d{3}-\d{4}$/; /* This is regular expression to allow only the strings in (nnn) nnn-nnnn format */ if( !link.value.match( phoneRegex ) ) { alert( 'Please enter phone number in (nnn)nnn-nnnn format' ); link.focus(); link.select(); return; } else {promptButtonFinish();} } } } /* Below is standard code to get FormWarpRequest*/ var fW = (typeof getFormWarpRequest == "function" ?getFormWarpRequest() : document.forms["formWarpRequest"]); if ( !fW || fW == undefined) { fW = ( formWarpRequest_THIS_ ?formWarpRequest_THIS_ : formWarpRequest_NS_ );} /* This returns all elements of Button tag */var buttons = fW.getElementsByTagName("BUTTON"); for (var i=0; i<buttons.length; i++) { if (buttons[i].id.match(/finish/)) // Capture the finish button { if (buttons[i].onclick.toString().indexOf('finish') > 0) { buttons[i].onclick = ValidatePage;} /* This overrides the FINISH button and attaches it to our function */ } } </script>
How it works...
We first define a function called ValidatePage()
that captures the textbox value and checks whether it follows the required format. We are using the match function of JavaScript which allows us to parse the textbox string against our regular expression. The regular expression ^\(\d{3}\) \d{3}-\d{4}$
allows only the string in (nnn) nnn-nnnn format. You can read more about regular expressions on the Internet and also try some on this website: http://www.regular-expressions.info/javascriptexample.html
If the textbox value matches with our regular expression, we call the promptButtonFinish()
function to submit the prompt page. Otherwise, we show an error message and set the focus back to the textbox.
Finally, this ValidatePage()
function is attached to the Finish button by second part of the script. We capture the Finish button by its TagName ("button") and Id match (/finish/
), and then override its OnClick event.
- 突破平面Photoshop UI界面設計與制作剖析
- 玩轉微信
- VR新未來
- WordPress 2.7 Cookbook
- 零基礎玩轉AI繪畫
- Oracle VM Manager 2.1.2
- Alice 3 Cookbook
- 專業級音樂制作理論與實踐Pro Tools:從入門到應用
- AutoCAD 2016入門與提高(超值版)
- iPad Procreate風格繪畫之美
- Moodle Course Conversion: Beginner's Guide
- Premiere Pro CC 2015中文版基礎與實例教程(第4版)
- Origin 2022科學繪圖與數據分析
- 零基礎學Premiere Pro短視頻制作
- 科技繪圖/科研論文圖/論文配圖設計與創作自學手冊:科研動畫篇