0

I have a textbox on GUI which takes an "Mobile Number" as input. I want to validate it to find out if it has some characters, which would mean the number is invalid

So, a number 9876543210 is valid while a number 98765df013 is invalid

I made a array of all characters (which are not allowed by me)

string[] alphabeticChars       = new string[] {    
                                                                    "a", "b", "c", "d", "e", "f", "g",
                                                                    "h", "i", "j", "k", "l", "m", "n",
                                                                    "o", "p", "q", "r", "s", "t", "u",
                                                                    "v", "w", "x", "y", "z"
                                                               };

and then I wrote a validation function

public bool HasCharacters(string text)
{
    foreach(string character in this.alphabeticChars)
         if(text.Contains(character.ToLower()) || text.Contains(character.ToUpper())) 
                return true;

    return false;
}

As you can see, I need to call Contains twice, one for "Lower Case" and another time for "Upper Case". I checked and couldn't find ContainsIgnoreCase or something.

What is the best way to to such a thing ? (Don't mention regular expressions, as I don't want to use them)

4
  • 1
    I suppose I'll be the person that asks: What is the problem with regular expressions here?
    – jdphenix
    May 8, 2015 at 4:46
  • @jdphenix probably because everybody blindly follows this: blog.codinghorror.com/… (without completely reading) May 8, 2015 at 4:47
  • @jdphenix Nothing specific, just wanted to do them by myself.. regular expressions is always something can be handy.. so, first i wanted to explore by myself
    – SimpleGuy
    May 8, 2015 at 4:54
  • You should edit in your actual requirements to your question.
    – jdphenix
    May 8, 2015 at 4:54

6 Answers 6

5

In a very simple way :-)

foreach(string character in this.alphabeticChars)
     if(text.ToLower().Contains(character)) 
            return true;

Or maybe you can do a regular expression which is more efficient

2
  • Great !. Why on earth did I not think of it.. Thanks !
    – SimpleGuy
    May 8, 2015 at 4:41
  • I saw your edit, you don't need to convert your character to lower. You control the list of characters and you can take care to have ony lower case. May 8, 2015 at 4:45
2

My answer it is not exactly what you are asking, it more suggestion, better way to validate phone numbers is using of libphonenumber (c#) library https://libphonenumber.codeplex.com that is port of Google libphonenumber. I have found it as the best way to do it for me. You can validate numbers based on country code and to format them in output.

Update. Here is more up to date version of the library https://www.nuget.org/packages/libphonenumber-csharp/

2
  • Excellent suggestion. How up-to-date is that, considering that the last activity seems to have been Mon Sep 9, 2013 May 8, 2015 at 4:49
  • @BrendanGreen You can find it on nuget too nuget.org/packages/libphonenumber-csharp there is more up to date version.
    – genichm
    May 8, 2015 at 4:55
1

Try this:

bool isValid = !text.Any(c => Char.IsLetter(c));
1

My take, using Linq:

public static class StringExtensions
{
    public static bool ContainsOnlyDigits(this string s)
    {
        return s.All(c => c >= '0' && c <= '9');
    }
}

And in use:

var result = "12345678".ContainsOnlyDigits();
1
  • 1
    Could be s.All(Char.IsDigit) ;)
    – jdphenix
    May 8, 2015 at 4:45
1

Since you seem to want for test for digits (as your expected input is a "mobile number", it can be a one-liner:

bool isValid = text.All(Char.IsDigit);
2
  • Well that is a good approach, but tomorrow the requirement might to include special characters like - and +, in which case +91-9712712345 would be a valid result. What could be done in that case
    – SimpleGuy
    May 8, 2015 at 4:47
  • I would drop the requirement to not use a regular expression and use that.
    – jdphenix
    May 8, 2015 at 4:48
1

Instead of checking the presence of a character, switch that logic around and just verify that everything is a number:

public bool IsNumeric(string text)
{
    return text.All(char.IsDigit);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.