Get financial year using C#

Posted by Prince | 8:43:00 PM | | 0 comments »

       
Get financial year using C# :

// The following C# function used to get the Financial year from the given date


       public static string GetFinancialYear(string cdate)
        {
            string finyear = "";
            DateTime dt = Convert.ToDateTime(cdate);
            int m = dt.Month;
            int y = dt.Year;
            if (m > 3)
            {
                finyear = y.ToString().Substring(2, 2) + "-" + Convert.ToString((y + 1)).Substring(2,2);   
                //get last  two digits (eg: 10 from 2010);
            }
            else
            {
                finyear = Convert.ToString((y - 1)).Substring(2, 2) + "-" + y.ToString().Substring(2, 2);
            }
            return finyear;
       }



Read more ...

Watermark TextBox using JavaScript

Posted by Prince | 8:40:00 PM | | 0 comments »

This article, will explain how watermark textbox using JavaScript.
 It is very useful and uses less resourcescompared to AJAX.
Below is the short JavaScriptthat helps doing the watermark. 
Code:
<script type = "text/javascript">
   
var defaultText = "Enter your text here";    
function waterMarkText(txt, evt)     
{      
if(txt.value.length == 0 && evt.type == "blur")       
{           
txt.style.color = "red";           
txt.value = defaultText;       
}       
if(txt.value == defaultText && evt.type == "focus")        
{           
txt.style.color = "green";           
txt.value="";        
}
}
</script>

The script will be called on onblur and onfocus events of the textbox. The
script simply does the following two checks
1. If the textbox is empty and the event type is blur event it sets the watermark and changes
the font color as Gray.
2. If the textbox text matches default text and the event type is the focus it clears
the textbox and sets the font color as Black. 
Now we just call it withthe textbox. 
<asp:TextBox ID="TxtWaterMark" runat="server" Text= "Enter your text here"
 ForeColor = "Gray" onblur = "waterMarkText(this, event);"    
onfocus = "waterMarkText(this, event);"> </asp:TextBox>

Now write the following code in to the code behind. 
C#
TxtWaterMark.Attributes.Add("onblur", "waterMarkText(this,event);");
TxtWaterMark.Attributes.Add("onfocus", "waterMarkText(this,event);");   


Watermark Demo:

Watermark TextBox    




Read more ...

Hai friends,
There are many ways to detect if a String contains Special Characters. In this code snippet, I will show a quick way to detect special characters using Regex.

C# code for find  if a String contains Special Characters.

static void Main(string[] args)
{
    string str = "Th!s $tri^g c@n$ist $pecial ch@rs";
    Match match = Regex.Match(str, "[^a-z0-9]",
            RegexOptions.IgnoreCase);
    while (match.Success)
    {
        string key = match.Value;
        Console.Write(key);
        match = match.NextMatch();
    }
    Console.ReadLine();
}

Read more ...

Get IP Address Of A System using c# :

  The following just two line code useful for Get IP Address Of A Machine.

        // Get the hostname 
        string yourHost = System.Net.Dns.GetHostName();

        // Show the hostname 
        lblMyhost.Text = yourHost;

        // Get the IP from the host name
        string yourIP = System.Net.Dns.GetHostEntry(yourHost).AddressList[0].ToString();

        // Show the IP 
         lblIPAddress.Text = yourIP;

Read more ...

Related Posts Plugin for WordPress, Blogger...