Advertisements

Archive

Posts Tagged ‘Parsing’

Validate INDIAN PAN Number Using Regular expression

November 29, 2012 Leave a comment

Indian PAN is as follows: AAAAA9999A:
Where First five characters are letters, next 4 numerals, last character letter
One rule there the fourth character is choosen from a list Alphabates as bellows.

C – Company
P – Person
H – HUF(Hindu Undivided Family)
F – Firm
A – Association of Persons (AOP)
T – AOP (Trust)
B – Body of Individuals (BOI)
L – Local Authority
J – Artificial Juridical Person
G – Govt

http://en.wikipedia.org/wiki/Permanent_account_number

Hence I will create a regular expression
as bellow

^[\w]{3}(p|P|c|C|h|H|f|F|a|A|t|T|b|B|l|L|j|J|g|G)[\w][\d]{4}[\w]$

Here I have a textbox and a regular expression validator in my aspx page as bellow.

<asp:TextBox ID="txtPan" runat="server" ></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" 

     ControlToValidate="txtPan" runat="server" ErrorMessage="Invalid PAN"      ValidationExpression="^[\w]{3}(p|P|c|C|h|H|f|F|a|A|t|T|b|B|l|L|j|J|g|G)[\w][\d]{4}[\w]$">

</asp:RegularExpressionValidator>
Advertisements

Useful tips while using bool.TryParse method

November 23, 2012 Leave a comment

Case I:

bool temp = bool.TryParse(“False”, out temp);

Console.WriteLine(temp);

Will always return true

Because bool.TryParse return true if string can be parsed into a bool and

the second parameter to TryParse is an out parameter the TryParse method is forced to initialize the parameter.

And after that we initialize same parameter with the value return by bool.TryParse so it will be set as true.

Case II:

bool temp;

bool.TryParse(“False”, out temp);

Console.WriteLine(temp);

In this case it will return false.

Hope this help!