<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Chakravarthy's Space : JavaScript</title><link>http://msmvps.com/blogs/chakravarthy/archive/tags/JavaScript/default.aspx</link><description>Tags: JavaScript</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>CheckBoxList validation</title><link>http://msmvps.com/blogs/chakravarthy/archive/2009/05/21/checkboxlist-validation.aspx</link><pubDate>Thu, 21 May 2009 05:04:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1692892</guid><dc:creator>Chakravarthy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/chakravarthy/rsscomments.aspx?PostID=1692892</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/chakravarthy/commentapi.aspx?PostID=1692892</wfw:comment><comments>http://msmvps.com/blogs/chakravarthy/archive/2009/05/21/checkboxlist-validation.aspx#comments</comments><description>&lt;p&gt;Within our current project, we faced a requirement such that there are multiple checkboxes and the user is required to select any one of them (at least one of them). So as there are multiple checkboxes, decided to go with CheckBoxList control. Now the problem is that, we need to show a error message if the user is not selected any one of them. The problem also extends not just there, but the display text should be culture specific.&lt;/p&gt;  &lt;p&gt;So the story started long after I&amp;#39;ve written this post. But editing this entire post due to a simple non supportive feature by IE. I’ll come to that point later in this post, let me first detail the requirement and solution. There is one more solution for this unsupported error. Please read thru the post.&lt;/p&gt;  &lt;p&gt;For all the client side validations, it is widely known that validation controls supported by ASP.NET framework are popularly used. But these controls can’t handle the CheckBoxList. Hence the following solution.&lt;/p&gt;  &lt;p&gt;Step 1: Place your CheckBoxList control in a separate div as shown below. Observe that the text for these controls are populated from database depending upon the user culture&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;div &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;cblItems1&amp;quot;&amp;gt;
&lt;/span&gt;&lt;span style="color:blue;"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;CheckBoxList &lt;/span&gt;&lt;span style="color:red;"&gt;ID&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;cblOptions&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;runat&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;server&amp;quot;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ListItem &lt;/span&gt;&lt;span style="color:red;"&gt;Text&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="background:#ffee62;"&gt;&amp;lt;%&lt;/span&gt;$Resources:CommonFormFields, Repair&lt;span style="background:#ffee62;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ListItem&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ListItem &lt;/span&gt;&lt;span style="color:red;"&gt;Text&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="background:#ffee62;"&gt;&amp;lt;%&lt;/span&gt;$Resources:CommonFormFields, Calibration&lt;span style="background:#ffee62;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ListItem&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ListItem &lt;/span&gt;&lt;span style="color:red;"&gt;Text&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="background:#ffee62;"&gt;&amp;lt;%&lt;/span&gt;$Resources:CommonFormFields, RepairCalibration&lt;span style="background:#ffee62;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ListItem&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ListItem &lt;/span&gt;&lt;span style="color:red;"&gt;Text&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="background:#ffee62;"&gt;&amp;lt;%&lt;/span&gt;$Resources:CommonFormFields, OnlineService&lt;span style="background:#ffee62;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;ListItem&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;CheckBoxList&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Step 2: Use ASP:Lable and get the error message while loading and place in a separate div and hide this div by default as mentioned below. Please note the div ID as &lt;font color="#0000ff"&gt;cblError1&lt;/font&gt; and the style is set to hidden by default&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;div &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;cblError1&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;style&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="color:red;"&gt;visibility&lt;/span&gt;: &lt;span style="color:blue;"&gt;hidden&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Label &lt;/span&gt;&lt;span style="color:red;"&gt;ID&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;lblReq1&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;runat&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;Text&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="background:#ffee62;"&gt;&amp;lt;%&lt;/span&gt;$Resources:CommonFormFields, SelectAny&lt;span style="background:#ffee62;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Label&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Step 3: Write a JScript function that reads all the client side rendered elements as &lt;font color="#0000ff"&gt;input&lt;/font&gt; objects and checks whether any of them are checked or not. Code explains more in detail&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;function &lt;/span&gt;CheckListValidation(cblItm, errDiv)
{
&lt;span style="color:green;"&gt;    &lt;/span&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;tDiv = document.getElementById(cblItm);
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;chkitems = tDiv.childNodes[1];
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;chkitm = chkitems.getElementsByTagName(&lt;span style="color:#a31515;"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;);
    &lt;span style="color:blue;"&gt;for &lt;/span&gt;(&lt;span style="color:blue;"&gt;var &lt;/span&gt;i = 0; i &amp;lt; chkitm.length; i++) 
    {
        &lt;span style="color:blue;"&gt;if &lt;/span&gt;(chkitm.checked) 
        {                    
                &lt;span style="color:blue;"&gt;return true&lt;/span&gt;;
        }
    }
    document.getElementById(errDiv).style.visibility = &lt;span style="color:#a31515;"&gt;&amp;#39;visible&amp;#39;&lt;/span&gt;;
    &lt;span style="color:blue;"&gt;return false&lt;/span&gt;;
}&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Step 4: Now it is the turn of invoking this function from the button click. Every button is having a method called as “OnClientClick” along with Click. Invoke this function from that method as mentioned below&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="color:blue;"&gt;:&lt;/span&gt;&lt;span style="color:#a31515;"&gt;Button &lt;/span&gt;&lt;span style="color:red;"&gt;ID&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;btnAddMore&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;runat&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;Text&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;Add More&amp;quot; 
&lt;/span&gt;&lt;span style="color:red;"&gt;OnClientClick&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;return CheckListValidation(&amp;#39;cbOptions&amp;#39;,&amp;#39;cblError1&amp;#39;);&amp;quot;
    &lt;/span&gt;&lt;span style="color:red;"&gt;OnClick&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;btnAddMore_Click&amp;quot; /&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Step 5: All setup and the code is running fine. But there is a problem with this code while running in IE. IE 6 and above doesn’t support the getElementsByTag &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;vTe = document.getElementsByTagName(..);&lt;/pre&gt;
There is a long story for this. Let me post one more post for the better mechanism of CheckBoxList Validation. Until then, what are your comments?? 

&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1692892" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/chakravarthy/archive/tags/Tips/default.aspx">Tips</category><category domain="http://msmvps.com/blogs/chakravarthy/archive/tags/Code/default.aspx">Code</category><category domain="http://msmvps.com/blogs/chakravarthy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://msmvps.com/blogs/chakravarthy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/chakravarthy/archive/tags/VS2008/default.aspx">VS2008</category><category domain="http://msmvps.com/blogs/chakravarthy/archive/tags/.NET/default.aspx">.NET</category></item><item><title>The importance of 'Return' keyword at JavaScript</title><link>http://msmvps.com/blogs/chakravarthy/archive/2007/09/15/the-importance-of-return-keyword-at-javascript.aspx</link><pubDate>Sat, 15 Sep 2007 12:26:53 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1198949</guid><dc:creator>Chakravarthy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/chakravarthy/rsscomments.aspx?PostID=1198949</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/chakravarthy/commentapi.aspx?PostID=1198949</wfw:comment><comments>http://msmvps.com/blogs/chakravarthy/archive/2007/09/15/the-importance-of-return-keyword-at-javascript.aspx#comments</comments><description>&lt;p&gt;Recently, while coding for a Text Field value padded with left zeros, realised the importance of the RETURN key word for the FUNCTION written in JavaScript. Before i mention the actual importance, let me describe you the situation.&lt;/p&gt; &lt;p&gt;Scenario: A Text box need to be padded with zeros and should have the length of 7 digits, even the data entered is less than 7.&lt;/p&gt; &lt;p&gt;Ex: When the key board input being 88, the text box should show &lt;strong&gt;00000&lt;/strong&gt;88. Note the ZEROs padded on left.&lt;/p&gt; &lt;p&gt;So, started with a JavaScript function as mentioned below&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span&gt;function&lt;/span&gt; PadZeros(x)
    {
        &lt;span&gt;var&lt;/span&gt; v = x.value;
        &lt;span&gt;while&lt;/span&gt;(v.length&amp;lt;7)
        {
            v = &lt;span&gt;&amp;#39;0&amp;#39;&lt;/span&gt; + v;
        }
        &lt;span&gt;var&lt;/span&gt; ss = document.getElementById(x.id);
        ss.value = v;
    }&lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;After this, the text box is padding with ZEROs and the code is perfectly running. To allow this to code execute for any given text box, all you have to do is, add the ATTRIBUTE to that text box. And while adding keep one thing in mind that, we would be calling this function on BLUR, ie., LOST FOCUS of the text box. The code is as mentioned below.&lt;/p&gt;&lt;pre class="code"&gt;            &lt;span&gt;this&lt;/span&gt;.txtPCode.Attributes.Add(&lt;span&gt;&amp;quot;onblur&amp;quot;&lt;/span&gt;, &lt;span&gt;&amp;quot;PadZeros(this);&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Please note the &amp;#39;this&amp;#39; keyword. The usage of &amp;#39;this&amp;#39; keyword has many possibilities. Let me see that, one day will post where the &amp;#39;this&amp;#39; keyword is used and their context. And also note that, neither the function is returning any value nor the text box is added with the code that handles the output of the function. Will come to that in short.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Every thing is working perfectly well and going on smooth. But suddenly, i realised that the text box is just padding ZEROs when there is no Input. I see all ZEROs in the text box as &lt;strong&gt;0000000&lt;/strong&gt;. Then came the real trick to the function.&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span&gt;function&lt;/span&gt; PadZeros(x)
    {
        &lt;span&gt;var&lt;/span&gt; v = x.value;
        &lt;span&gt;if&lt;/span&gt;(v.length == 0)
        {
            &lt;span&gt;var&lt;/span&gt; vTe = document.getElementById(x.id);
            vTe.focus();
            alert(&lt;span&gt;&amp;#39;Please enter Provider Code .. &amp;#39;&lt;/span&gt;);
        }
        &lt;span&gt;while&lt;/span&gt;(v.length&amp;lt;7)
        {
            v = &lt;span&gt;&amp;#39;0&amp;#39;&lt;/span&gt; + v;
        }
        &lt;span&gt;var&lt;/span&gt; ss = document.getElementById(x.id);
        ss.value = v;
    }&lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;What do you see here is the mechanism to set the focus back to the text box. Great... but that even is not solving my purpose of leaving the text box blank when there is no input. This function is still adding ZEROs to no input and showing all ZEROs. Then came the purpose of the &lt;strong&gt;&lt;font color="#0080ff"&gt;&amp;#39;return&amp;#39;&lt;/font&gt;&lt;/strong&gt; keyword. The entire requirement is simply solved by this keyword. All i&amp;#39;ve done is.. changed the code as mentioned below.&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span&gt;function&lt;/span&gt; PadZeros(x)
    {
        &lt;span&gt;var&lt;/span&gt; v = x.value;
        &lt;span&gt;if&lt;/span&gt;(v.length == 0)
        {
            &lt;span&gt;var&lt;/span&gt; vTe = document.getElementById(x.id);
            vTe.focus();
            alert(&lt;span&gt;&amp;#39;Please enter Provider Code .. &amp;#39;&lt;/span&gt;);
            &lt;span&gt;return&lt;/span&gt; &lt;span&gt;false&lt;/span&gt;;
        }
        &lt;span&gt;while&lt;/span&gt;(v.length&amp;lt;7)
        {
            v = &lt;span&gt;&amp;#39;0&amp;#39;&lt;/span&gt; + v;
        }
        &lt;span&gt;var&lt;/span&gt; ss = document.getElementById(x.id);
        ss.value = v;
        &lt;span&gt;return&lt;/span&gt; &lt;span&gt;true&lt;/span&gt;;
    }&lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;At the code behind added &amp;#39;return&amp;#39; as shown here &lt;/p&gt;&lt;pre class="code"&gt;            &lt;span&gt;this&lt;/span&gt;.txtPCode.Attributes.Add(&lt;span&gt;&amp;quot;onblur&amp;quot;&lt;/span&gt;, &lt;span&gt;&amp;quot;return PadZeros(this);&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;That&amp;#39;s all.. hoollaa... What do you say ?&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a8c92c9f-3426-4b59-949b-0a4912605601" style="padding-right:0px;display:inline;padding-left:0px;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/JavaScript" rel="tag"&gt;JavaScript&lt;/a&gt;, &lt;a href="http://technorati.com/tags/PadZeros" rel="tag"&gt;PadZeros&lt;/a&gt;, &lt;a href="http://technorati.com/tags/return" rel="tag"&gt;return&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1198949" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/chakravarthy/archive/tags/First/default.aspx">First</category><category domain="http://msmvps.com/blogs/chakravarthy/archive/tags/Hyderabad/default.aspx">Hyderabad</category><category domain="http://msmvps.com/blogs/chakravarthy/archive/tags/Tips/default.aspx">Tips</category><category domain="http://msmvps.com/blogs/chakravarthy/archive/tags/Code/default.aspx">Code</category><category domain="http://msmvps.com/blogs/chakravarthy/archive/tags/InteQ/default.aspx">InteQ</category><category domain="http://msmvps.com/blogs/chakravarthy/archive/tags/JavaScript/default.aspx">JavaScript</category></item></channel></rss>