Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI Experts,

I am Doing a Windows application in C#, And i am stuck in a validation process.
I want to check the value in a text box is numeric or not. But one condition is Not only the numeric but also it supports "+" sign....

Here i ahev used the Regx method... Here is the below sample code...

C#
Regex.IsMatch(txt_Country_Code.Text.Trim(), "^[0-9]+$")


But now it supports only the numbers.. i need , it include the numeric also it suport "+" sign..


Plz help me to solve this problem..


Thnaks

Dileep
Posted
Comments
Jegan Thiyagesan 20-Mar-13 11:07am    
you could stop the user from typing anything other than number or + in the text box if you want, this will eliminate need for regex at later stage.

"^\\+?[0-9]+$"

http://msdn.microsoft.com/en-us/library/az24scfc.aspx[^]

And we have a Regular Expessions forum here.
 
Share this answer
 
Comments
dilzz 20-Mar-13 11:35am    
Thanks.. its working..
Check this: ^[+\-]?\d+$
 
Share this answer
 
Try first to describe the text pattern in words:
1) optinal leading sign
2) followed by a series of digits (at least one)
3) there may be leading and trainling spaces, but no space between the sign and the digits and not between the digits

In Pseudo code:
@begin[space]*[sign]?[digit]+[space]*@end

(* = 0... times, ? = 0 or 1 times, + = 1 ... times)

Now you have to translate into regex syntax:


@begin^
@end$
[space]\s
[sign][-+]
[digit]\d


Place all into a verbatim string (@"...") so that you do not have to write \\ for a single \.
C#
@"^\s*[-+]?\d+\s*$"


Notes:

  1. see Escaping in C#: characters, strings, string formats, keywords, identifiers[^] for verbatim strings and their use.
  2. The regex sign is a bit tricky: you must have the - as first charater in the character group. In this way, the - is not interpreted as range meta character (e.g. like in [0-9a-fA-F])


Cheers
Andi
 
Share this answer
 
Hi,
you can use the regular expression or you can catch the key press event and stopping form use typing unwanted keys.

C#
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((!char.IsNumber(e.KeyChar)) && (!char.IsSymbol(e.KeyChar )))
    {
        e.Handled = false;
    }
}


If the user press on any key that is not a number or a symbol, then the handle will be rejected, so the text box will not accept that key.

If you are specifically looking for that "+" symbol, then the code will be

C#
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((char.IsNumber(e.KeyChar)) || (e.KeyChar == '+'))
    {
        e.Handled = true;
    }
    else
    {
        e.Handled = false;
    }
}


I hope this helps.

Regards
Jegan
 
Share this answer
 
Comments
PIEBALDconsult 20-Mar-13 11:10am    
Except you don't want a + after a digit, or more than one.
Jegan Thiyagesan 21-Mar-13 5:28am    
Why not? The author did not specifically say that there should be only one + symbol and the location that the symbol.
PIEBALDconsult 21-Mar-13 10:24am    
"1) optinal leading sign"
Jegan Thiyagesan 22-Mar-13 6:26am    
Again this is an assumption you are making, neither of us know what is the authors requirement.
Any how, In my answer I was providing an alternative option, that instead of allowing the user to enter some inputs and validating them at the end, why not stop the user from entering invalid inputs. If there is a requirement to have the '+' in particular location, it can also be verified in the key pressed event; that is up to the developer.
It is just an alternative way of approaching this issue.
Andreas Gieriet 20-Mar-13 12:07pm    
This allows for 0+1+2 etc, which is probably not the intension.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900