Validate Indian Currency using Regular Expression

Posted by NamrataC under WPF on 9/13/2010 | Points: 10 | Views : 12938 | Status : [Member] | Replies : 8
Hi All,
I'm new to WPF. I want to validate amount field in my project using regular expression.
Here I want to allow user to enter only 1 decimal point.
Please help to resolve the issue.




Responses

Posted by: T.saravanan on: 9/13/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

^[0-9]{0,3}.?[0-9]{1}$


use this Regular Expression to Solve the issue.

Here {0,3} --> No.of Integer (No.of Count before decimal point)
{1} --> After decimal point (No.of Count)

Cheers :)

Thanks,
T.Saravanan

NamrataC, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: NamrataC on: 9/13/2010 [Member] Starter | Points: 25

Up
0
Down
Here decimal point is compulsory but I don't want decimal point to be compulsory.

NamrataC, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 9/13/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hey,
Here I want to allow user to enter only 1 decimal point. --> It means...


Thanks,
T.Saravanan

NamrataC, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: NamrataC on: 9/13/2010 [Member] Starter | Points: 25

Up
0
Down
It meant that if decimal entered only single decimal is to be entered. Also this expression allows alphabetic values too.
I want only numeric values i.e. Indian Currency.

NamrataC, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 9/13/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,
using Regex Condition You Can solve.

if(!Regex.IsMatch(txtBox.Text,@"^[0-9]{0,3}.?[0-9]{1}$"))

{
// Your Error Message
}
else
{
// Allow to Next Level
}


// if Regex allow alphabetic value means Just check one Valid Method

Private bool IsValidValue()

{
bool bVal=false;
string[ ] sValue=txtBox.Text.Split('.');
try
{
for(int nlen=0;nlen<sValue.Length;nlen++)
{
if(int.Parse(sValue[nlen].ToString())>=0) // If you need change this Condition
{
bVal=true;
}
}
}
catch(Exception)
{
bVal=false
}
return bVal;
}


Just check this bool method in your event. (
Ex:
if(! IsValidValue())
{
// Error Message
}

Check this Code.

Thanks,
T.Saravanan

NamrataC, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: NamrataC on: 9/13/2010 [Member] Starter | Points: 25

Up
0
Down
I meant your expression @"^[0-9]{0,3}.?[0-9]{1}$" is allowing alphabetic values which is not valid in currency field.

NamrataC, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 9/13/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Ya,
My Solution is only for to check the value is numeric or not.
If you want textbox only allow to enter a numeric value means,
to create a user control for numeric textbox.what ever condition you want means you can included in this user control.

Thanks,

Thanks,
T.Saravanan

NamrataC, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: NamrataC on: 9/13/2010 [Member] Starter | Points: 25

Up
0
Down
Okey. I thought this can be done by handling validation.
I have used
'@"[^\.\d]'
this expression but it allows multiple decimal points

NamrataC, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response