Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# regular-expression , +
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...
 
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 20-Mar-13 5:38am
dilzz1.2K
Comments
Jegan Thiyagesan at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

"^\\+?[0-9]+$"
 
http://msdn.microsoft.com/en-us/library/az24scfc.aspx[^]
 
And we have a Regular Expessions forum here.
  Permalink  
Comments
dilzz at 20-Mar-13 11:35am
   
Thanks.. its working..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Check this: ^[+\-]?\d+$
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 \.
@"^\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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi,
you can use the regular expression or you can catch the key press event and stopping form use typing unwanted keys.
 
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
 
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
  Permalink  
Comments
PIEBALDconsult at 20-Mar-13 11:10am
   
Except you don't want a + after a digit, or more than one.
Jegan Thiyagesan at 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 at 21-Mar-13 10:24am
   
"1) optinal leading sign"
Jegan Thiyagesan at 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 at 20-Mar-13 12:07pm
   
This allows for 0+1+2 etc, which is probably not the intension.
Cheers
Andi
Jegan Thiyagesan at 21-Mar-13 5:28am
   
Are you sure?

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

  Print Answers RSS
0 Mathew Soji 270
1 BillWoodruff 260
2 Sergey Alexandrovich Kryukov 240
3 OriginalGriff 236
4 Afzaal Ahmad Zeeshan 208
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 20 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100