Sunday, November 13, 2011

Validators Control to allow only Integers

Here are few different ways you can use native .NET validation controls to validate to allow only integers. We can do it in two ways one by using regular expression and one with compare validators

Method 1: Compare Validator
<asp:TextBox ID="NewMobileTextBox" runat="server" Width="280px" autocomplete="off"
CssClass="signuptxtbox" ></asp:TextBox>
<asp:RequiredFieldValidator ID="NewMobileRequired" runat="server" ControlToValidate="NewMobileTextBox"
CssClass="failureNotification" Display="Dynamic" ErrorMessage="New Mobile is required." ToolTip="New Mobile is required." ValidationGroup="ChangeNewMobileValidationGroup">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="NewMobileCompare" runat="server" ControlToValidate="NewMobileTextBox" CssClass="failureNotification" Display="Dynamic" Operator="DataTypeCheck" ErrorMessage="Mobile Number should be numeric only." ValidationGroup="ChangeNewMobileValidationGroup" Type="Integer">*</asp:CompareValidator>


Method 2: Regular Expression Validator

<asp:TextBox ID="NewMobileTextBox" runat="server" Width="280px" autocomplete="off"
CssClass="signuptxtbox" ></asp:TextBox><asp:RequiredFieldValidator ID="NewMobileRequired" runat="server" ControlToValidate="NewMobileTextBox"
CssClass="failureNotification" Display="Dynamic" ErrorMessage="New Mobile is required." ToolTip="New Mobile is required." ValidationGroup="ChangeNewMobileValidationGroup">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="mobileRGEX" runat="server"
ControlToValidate="NewMobileTextBox" CssClass="failureNotification"
ErrorMessage="Please Enter Only Numbers" ValidationExpression="^\d+$"
ValidationGroup="ChangeNewMobileValidationGroup"></asp:RegularExpressionValidator>

Saturday, November 12, 2011

Hot Fix For Windows 7 Start Menu Search Not Working

Sometimes in windows 7 start menu search will not work. Many blogs will tell you to edit the registry. But it is not recommended. Microsoft released the hotfix for this. Download at: http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=977380&kbln=en-us

Wednesday, November 02, 2011

Regex validate Email address C#

This is how we can validating email address through C# using Regular Expression. Here is the sample code,

using System.Text.RegularExpressions;



public bool vaildateEmail(string useremail)
{
bool istrue = false;
Regex reNum = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
CaptureCollection cc = reNum.Match(useremail).Captures;
if (cc.Count == 1)
{
istrue = true;
}
return istrue;
}

Delete All Files using C#

For Deleting all the files first you need to get the list of file names from the specified directory (using static method Directory.Get­Files. Then delete all files from the list.
Method 1
using System.IO;

string[] filePaths = Directory.GetFiles(@"c:\Directory\");
foreach (string filePath in filePaths)
  File.Delete(filePath)

Method 2

To Delete all files using one code line, you can use Array.ForEach with combination of anonymous method.

Array.ForEach(Directory.GetFiles(@"c:\MyDirectory\"),delegate(string path) 
{ File.Delete(path); });

How to get current page Filename using C#

There are different ways to get current page filename using c#. Here are 3 methods you can use for your advantage.

Method 1
string currentPageFileName = new FileInfo(this.Request.Url.LocalPath).Name;

Method 2

string sPath = HttpContext.Current.Request.Url.AbsolutePath;
string[] strarry = sPath.Split('/');
int lengh = strarry.Length;
string sReturnPage = strarry[lengh - 1];

Method 3

string absPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo finfo = new System.IO.FileInfo(absPath);
string fileName = finfo.Name;

 

Out of these I like Method 1 and 3 as its straight forward. Use a per your advantage. Good luck.