none
Telephone Number Input Validation RRS feed

  • Question

  • The system we have in place at work for call centre agents to input telephone numbers doesn't validate their input, so, in a lot of cases, we have junk data being entered (0000000000 as a phone number generally). I've been taked with fixing this, but I'm not sure how to stop the agents entering junk like this.

    At first I figured I'd be alright with a standard condition to check if the value could be parsed as an integer, and then a comparison of the value and 0000000000 and if they're the same, stop the operation, but then I realised... What's to stop them entering stuff like 0000000001? Validation by area code would work, but then they'll be able to enter (for my hometown) something like 0110000000 which isn't going to help either.

    I've looked at the Regular Expression Validator's phone numbers and there isn't a number in there for my country.

    Does anyone have any suggestions or links to resources that might be helpful?
    Logan Young, MCPD Web Developer hopeful
    Wednesday, November 25, 2009 10:08 AM

Answers

  • u can implement Regular Expression (RegEx) to validate the number... Please check the below site, they have given some samples...

    u can make use of it...

    Phone Number ^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$ (425) 555-0123
    425-555-0123
    425 555 0123
    1-425-555-0123
    Validates a U.S. phone number. It must consist of 3 numeric characters, optionally enclosed in parentheses, followed by a set of 3 numeric characters and then a set of 4 numeric characters.


    http://www.dotnetspider.com/resources/31104-Regular-Expression-for-validating-phone-number.aspx
    http://www.csharphelp.com/archives/archive21.html


    Narayanan Dayalan - Zeetaa Business Solutions ------- Please "Mark As Answer", if my answer works well with ur Query
    • Proposed as answer by Cor Ligthert Wednesday, November 25, 2009 11:33 AM
    • Unproposed as answer by LoganY Wednesday, November 25, 2009 2:09 PM
    • Proposed as answer by Cor Ligthert Thursday, November 26, 2009 5:31 AM
    • Marked as answer by Jeff Shan Monday, November 30, 2009 2:04 AM
    Wednesday, November 25, 2009 11:10 AM
  • public and static are the keywords common in C# and also in VB .net, there is nothing complicated, and RegEx dont differ in these both a languages... any how, here is the code in VB.NET


    using System;
    using System.Text.RegularExpressions;

    class MainClass
    {
    public static void Main()
    {
    string text = "(800) 888-1211\n" +
    "(212) 555-1212\n" +
    "(506) 777-1213\n" +
    "(650) 222-1214\n" +
    "(888) 111-1215\n";

    string areaCodeRegExp = @"(?\(\d\d\d\))";

    string phoneRegExp = @"(?\d\d\d\-\d\d\d\d)";

    MatchCollection myMatchCollection = Regex.Matches(text, areaCodeRegExp + " " + phoneRegExp);

    foreach (Match myMatch in myMatchCollection)
    {
    Console.WriteLine("Area code = " + myMatch.Groups["areaCodeGroup"]);
    Console.WriteLine("Phone = " + myMatch.Groups["phoneGroup"]);

    foreach (Group myGroup in myMatch.Groups)
    foreach (Capture myCapture in myGroup.Captures)
    Console.WriteLine("myCapture.Value = " + myCapture.Value);
    }
    }
    }
    Narayanan Dayalan - Zeetaa Business Solutions ------- Please "Mark As Answer", if my answer works well with ur Query
    • Proposed as answer by Cor Ligthert Thursday, November 26, 2009 5:29 AM
    • Marked as answer by Jeff Shan Monday, November 30, 2009 2:04 AM
    Thursday, November 26, 2009 5:11 AM
  • ok dont worry... here is the VB.NET version of it...  sorry if i made u to wait for long time...


     Imports System
     Imports System.Text.RegularExpressions

     Class MainClass
         Public Shared Sub Main()
             Dim text As String = "(800) 888-1211" & vbLf & "(212) 555-1212" & vbLf & "(506) 777-1213" & vbLf & "(650) 222-1214" & vbLf & "(888) 111-1215" & vbLf
           
             Dim areaCodeRegExp As String = "(?\(\d\d\d\))"
           
             Dim phoneRegExp As String = "(?\d\d\d\-\d\d\d\d)"
           
             Dim myMatchCollection As MatchCollection = Regex.Matches(text, (areaCodeRegExp & " ") + phoneRegExp)
           
             For Each myMatch As Match In myMatchCollection
                 Console.WriteLine("Area code = " & myMatch.Groups("areaCodeGroup"))
                 Console.WriteLine("Phone = " & myMatch.Groups("phoneGroup"))
               
                 For Each myGroup As Group In myMatch.Groups
                     For Each myCapture As Capture In myGroup.Captures
                         Console.WriteLine("myCapture.Value = " & myCapture.Value)
                     Next
                 Next
             Next
         End Sub
     End Class

    hope this helps u...


    Narayanan Dayalan - Zeetaa Business Solutions ------- Please "Mark As Answer", if my answer works well with ur Query
    • Proposed as answer by Cor Ligthert Thursday, November 26, 2009 10:29 AM
    • Marked as answer by Jeff Shan Monday, November 30, 2009 2:04 AM
    Thursday, November 26, 2009 9:21 AM

All replies

  • u can implement Regular Expression (RegEx) to validate the number... Please check the below site, they have given some samples...

    u can make use of it...

    Phone Number ^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$ (425) 555-0123
    425-555-0123
    425 555 0123
    1-425-555-0123
    Validates a U.S. phone number. It must consist of 3 numeric characters, optionally enclosed in parentheses, followed by a set of 3 numeric characters and then a set of 4 numeric characters.


    http://www.dotnetspider.com/resources/31104-Regular-Expression-for-validating-phone-number.aspx
    http://www.csharphelp.com/archives/archive21.html


    Narayanan Dayalan - Zeetaa Business Solutions ------- Please "Mark As Answer", if my answer works well with ur Query
    • Proposed as answer by Cor Ligthert Wednesday, November 25, 2009 11:33 AM
    • Unproposed as answer by LoganY Wednesday, November 25, 2009 2:09 PM
    • Proposed as answer by Cor Ligthert Thursday, November 26, 2009 5:31 AM
    • Marked as answer by Jeff Shan Monday, November 30, 2009 2:04 AM
    Wednesday, November 25, 2009 11:10 AM
  • Thanks Cor,

    I just have 1 problem. I don't code in C#, so I don't understand how all the public static void stuff works... Do you by any chance have links to Visual Basic examples???
    Logan Young, MCPD Web Developer hopeful
    Wednesday, November 25, 2009 2:11 PM
  • Just search for a C#-to-VB convertor webpage or, better, learn the basics of C# syntax.

    Most conversions from C# to VB are very simple, and once you've learned how to read C#, you'll be able to read that many more advice articles.
    Wednesday, November 25, 2009 2:36 PM
  • Take a look at this thread.

    VB.NET Format Phone Number



    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, November 25, 2009 2:54 PM
  • public and static are the keywords common in C# and also in VB .net, there is nothing complicated, and RegEx dont differ in these both a languages... any how, here is the code in VB.NET


    using System;
    using System.Text.RegularExpressions;

    class MainClass
    {
    public static void Main()
    {
    string text = "(800) 888-1211\n" +
    "(212) 555-1212\n" +
    "(506) 777-1213\n" +
    "(650) 222-1214\n" +
    "(888) 111-1215\n";

    string areaCodeRegExp = @"(?\(\d\d\d\))";

    string phoneRegExp = @"(?\d\d\d\-\d\d\d\d)";

    MatchCollection myMatchCollection = Regex.Matches(text, areaCodeRegExp + " " + phoneRegExp);

    foreach (Match myMatch in myMatchCollection)
    {
    Console.WriteLine("Area code = " + myMatch.Groups["areaCodeGroup"]);
    Console.WriteLine("Phone = " + myMatch.Groups["phoneGroup"]);

    foreach (Group myGroup in myMatch.Groups)
    foreach (Capture myCapture in myGroup.Captures)
    Console.WriteLine("myCapture.Value = " + myCapture.Value);
    }
    }
    }
    Narayanan Dayalan - Zeetaa Business Solutions ------- Please "Mark As Answer", if my answer works well with ur Query
    • Proposed as answer by Cor Ligthert Thursday, November 26, 2009 5:29 AM
    • Marked as answer by Jeff Shan Monday, November 30, 2009 2:04 AM
    Thursday, November 26, 2009 5:11 AM
  • public and static are the keywords common in C# and also in VB .net, there is nothing complicated, and RegEx dont differ in these both a languages... any how, here is the code in VB.NET


    Exactly Narayanan you save me the typing time.

    I found your first answer already extremely helpfull because it was not only for a local situation which we see mostly.

    My compliments and I hope that many persons vote both of your answer as helpful


    Success
    Cor
    Thursday, November 26, 2009 5:31 AM
  • OK, I guess it's about time to start looking at C#. I'm kind of reluctant to though, because I very much prefer VB, but if learning C# will help me learn VB, then I guess it's worth it.
    Thanks for the help Narayanan, I'll update this thread once I've tried it out.
    Logan Young, MCPD Web Developer hopeful
    Thursday, November 26, 2009 6:03 AM
  • Logan

    there is no reason for you to have to learn C# when asking a question in a VB.NET forum. Answers should be posted in VB.NET. I'm not saying you shouldn't learn another language - but you also shouldn't be forced to when you are not ready.

    to help you out here is a link to a C# to .NET converter

    http://www.developerfusion.com/tools/convert/csharp-to-vb


    Narayanan

    please refrain from posting code in a language other than VB.NET. If you want to post answers in C#, here is a link to the C# forum:

    http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/threads

    otherwise it would be really helpful if the OP says they don't know C#, please be considerate and repost your solution in VB.NET.


    Thank you

    Thursday, November 26, 2009 6:17 AM
  • ok dont worry... here is the VB.NET version of it...  sorry if i made u to wait for long time...


     Imports System
     Imports System.Text.RegularExpressions

     Class MainClass
         Public Shared Sub Main()
             Dim text As String = "(800) 888-1211" & vbLf & "(212) 555-1212" & vbLf & "(506) 777-1213" & vbLf & "(650) 222-1214" & vbLf & "(888) 111-1215" & vbLf
           
             Dim areaCodeRegExp As String = "(?\(\d\d\d\))"
           
             Dim phoneRegExp As String = "(?\d\d\d\-\d\d\d\d)"
           
             Dim myMatchCollection As MatchCollection = Regex.Matches(text, (areaCodeRegExp & " ") + phoneRegExp)
           
             For Each myMatch As Match In myMatchCollection
                 Console.WriteLine("Area code = " & myMatch.Groups("areaCodeGroup"))
                 Console.WriteLine("Phone = " & myMatch.Groups("phoneGroup"))
               
                 For Each myGroup As Group In myMatch.Groups
                     For Each myCapture As Capture In myGroup.Captures
                         Console.WriteLine("myCapture.Value = " & myCapture.Value)
                     Next
                 Next
             Next
         End Sub
     End Class

    hope this helps u...


    Narayanan Dayalan - Zeetaa Business Solutions ------- Please "Mark As Answer", if my answer works well with ur Query
    • Proposed as answer by Cor Ligthert Thursday, November 26, 2009 10:29 AM
    • Marked as answer by Jeff Shan Monday, November 30, 2009 2:04 AM
    Thursday, November 26, 2009 9:21 AM
  • jwavilla,

    I did not even see that it was C#, so sorry for that, but Narayanan first answer does not contain C# while the Op is then also complaining about that.

    In my idea was the first given complete for everybody who knows a little bit of VB.

    We are not unpaied coders for the world.


    Success
    Cor
    Thursday, November 26, 2009 10:31 AM
  • narayanan's VB.NET solution worked for me... thanks a lotz...

    Thursday, November 26, 2009 11:47 AM
  • >> The system we have in place at work for call centre agents to input telephone numbers doesn't validate their input, so, in a lot of cases, we have junk data being entered (0000000000 as a phone number generally). I've been tasked with fixing this, but I'm not sure how to stop the agents entering junk like this. <<

    You can stop them entering obvious junk, but once you do, they'll instead enter something like (812) 345 4567. This is an even greater problem because now all the incorrect data will not be at all obvious. This is a step backwards.

     

    Wednesday, October 19, 2011 6:53 AM