Friday, May 25, 2012

C# - Generate random password string

Use this method to generate random password. You can modify the characters length based on how complex you want the password to be and also decide which characters to use in the random string.



    private static string create_random_password()
    {
        string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
        char[] chars = new char[13];
        Random rd = new Random();

        for (int i = 0; i <= 13; i++)
        {
            chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
        }

        return new string(chars);
    }




Enjoy!

Tuesday, May 8, 2012

JavaScript - Limit the input length as you type, works with TextArea as well

Limit the input length as you type, works with TextArea as well:



<script type="text/javascript">
    function limit_text(limit_field, limit_num) {
        if (limit_field.value.length > limit_num) {
            limit_field.value = limit_field.value.substring(0, limit_num);
        }
    }
</script>


<asp:TextBox ID="tb_new_ad_body" runat="server" TextMode="MultiLine" onKeyPress=" limit_text (this,30);"></asp:TextBox>
                                        



Enjoy!

JavaScript - Only the first letter of the sentence capital as you type

A bit of tuning to some cool function that I found somewhere - now it's making the input text from lowercase to first letter capital all the rest are lowercase. My part of this neat function is that even if you type in all the letters capital still the result will be first letter capital and all the rest lowercase.



<asp:TextBox ID="tb_01" runat="server" onblur="this.value = this.value.substr(0, 1).toUpperCase() + this.value.substr(1).toLowerCase();"></asp:TextBox>



Enjoy!

Friday, May 4, 2012

HTML/ASP.NET - How to check if JavaScript disabled without JavaScript function

Using JavaScript function to check if JavaScript is disabled and display message stating so may some time corrupt other JavaScript or JQuery stuff on our page.
Before I was using this structure:



    <script language="javascript" type="text/javascript">
        window.onload = function () { document.getElementById('entire_content').style.display = ''; };
    </script>

<form id="form_01" runat="server">
<div id="entire_content" style="display:none;">
My content
</div>
</form>
 <noscript>
JavaScript is disabled!
</asp:Panel>
But after today I'm using much simpler approach:

<form id="form_01" runat="server">
<div id="entire_content">
My content
</div>
</form>

<noscript>
    <style type="text/css">
       #entire_content { display:none; }
    </style>
JavaScript is disabled!
</noscript>


Enjoy!

CSS - Control appearance of horizontal and vertical scrollbars

We can separately control the show/scroll/hide the vertical and the horizontal scrollbars in CSS, for example:



overflow-y:hiddenoverflow-x:scroll;

Enjoy!

ASP.NET - Set min and max number of characters using Regular Expression

We can set the min and max number of characters for input fields using Regular Expression (Regex for short). For example I use it with RegularExpressionValidator in ASP.NET MultiLine TextBoxes (which from some reason don't cooperate with nested MaxLength control property).
In this example I want to sent min as 8 and max as 16 characters, alphanumeric only (to add additional special characters type them between the "9" and the "\s"):

ValidationExpression=" ^[a-zA-Z0-9\s]{8,16}$ "

Enjoy!

CSS - Rounded corners for all major browsers

Works on all major browsers - IE, FF, Chrome, Opera, Safari


  -moz-border-radius: 20px;
  -webkit-border-radius: 20px;
  -khtml-border-radius: 20px;
  border-radius: 20px;


Enjoy!