Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public static bool hasValidCharforContactNo(string input)
        {
            Regex r = new Regex("^[0-9]{10}$");
            if (r.IsMatch(input))
            {
                return false;
            }

            return true;
        }


What I have tried:

i am trying to use above code but it is not working properly please give me proper code
Posted
Updated 19-Sep-18 20:30pm
v2
Comments
Richard Deeming 20-Sep-18 9:16am    
"it is not working properly"
Then explain what you expect it to do, and what it's actually doing.

"please give me proper code"
We can't do that until you explain what you expect the method to do.

try below code:

Regex: /^(\+\d{1,3}[- ]?)?\d{10}$/
1. ^ start of line
2. A + followed by \d+ followed by a  or - which are optional.
3. Whole point two is optional.
4. Negative lookahead to make sure 0s do not follow.
5. Match \d+ 10 times.
4. Line end.


it will not work for +910000000000
 
Share this answer
 
Here is a site for testing javascript regex expressions, with an example for 10 digits:
RegExr: Learn, Build, & Test RegEx[^]
^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)[1-9]((\s|\s?\-\s?)?[0-9])((\s|\s?-\s?)?[0-9])((\s|\s?-\s?)?[0-9])\s?[0-9]\s?[0-9]\s?[0-9]\s?[0-9]\s?[0-9]$

And another one for 11-12 digit numbers: RegExr: Learn, Build, & Test RegEx[^]

Here is an interesting article by the famous Expresso maker Jim Hollenhorst: The 30 Minute Regex Tutorial[^]

And here is the working code for Dutch phone numbers:
// using System.Text.RegularExpressions;

            string searchPattern = @"^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)[1-9]((\s|\s?\-\s?)?[0-9])((\s|\s?-\s?)?[0-9])((\s|\s?-\s?)?[0-9])\s?[0-9]\s?[0-9]\s?[0-9]\s?[0-9]\s?[0-9]$";
            var match = new Regex(searchPattern).Match(input);
            return (match.Success);

For other numbers, try this:
input = input.Replace("(", string.Empty).Replace(")", string.Empty).Replace("-", string.Empty).Replace(" ", string.Empty).Trim();
return Regex.IsMatch(input, "^[0-9]{10}$");
 
Share this answer
 
v8
Comments
Member 12183079 19-Sep-18 7:30am    
no only 10 digit please write for 10 digit
RickZeeland 19-Sep-18 7:35am    
Updated the solution !
Member 12183079 19-Sep-18 8:02am    
no not working
RickZeeland 19-Sep-18 8:23am    
Did you try the code at the bottom of my solution ? can you show me the input string you used ?
Please note that the example is for Dutch phone numbers which must begin with a 0 or + for international notation.
In order to help us figure out the problem, it is wise to include sample data that match the criteria and that don't.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]
 
Share this answer
 
v2
public static bool hasValidCharforContactNo(string input)
        {
            Regex r = new Regex(@"^\d{10}$");
            if (r.IsMatch(input))
            {
                return true;
            }else
{
            return false;
}
        }
I hope this will help you...
 
Share this answer
 
v2
Comments
Patrice T 20-Sep-18 5:19am    
I fear it will not help, the code is wrong.
RickZeeland 20-Sep-18 9:01am    
You can update your solution, that's a lot easier to read :)
RickZeeland 20-Sep-18 9:48am    
No no, I mean use the "Improve solution" button !

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