Validate text field possibly with regular expression

I'm writing a web based GUI using C# with .NET framework 3.5.

I need to validate a text field.  It can be either:

a)      1 or 2 alphas with or without a colon

A
A:
AA
AA:

or

b)      Any of the above followed by two numbers delimited by /

for example
A/n/m  
A: /n/m
AA/n/m
AA: /n/m
AA:/6/8

n and m are each in the range 1 to 12 and n < m
AlHal2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kblauCommented:
do something like this....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace EEMatch
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.IsValid("A");
            p.IsValid("A");
            p.IsValid("B:");
            p.IsValid("CC");
            p.IsValid("CC:");
            p.IsValid("A2222");
            p.IsValid("B:222");
            p.IsValid("CC222");
            p.IsValid("CC:222");
            p.IsValid("A2");
            p.IsValid("2B:");
            p.IsValid("/1/2");
          
        }

        public bool IsValid(String s)
        {
            //now exclude second character if not colon, alpha, or nothing
            string secondChar = String.Empty;
            if (s.Length > 1)
            {
                secondChar = s.Substring(1, 1);
                Regex regexA = new Regex(@"[a-zA-Z:/]");
                Match match2 = regexA.Match(secondChar);
                if (!match2.Success)
                    return false;
            }
                        
            Regex regex = new Regex(@"^[a-zA-Z]{1}[a-zA-Z:/]?");
            Match match = regex.Match(s);
            if (match.Success)
            {
                return true;
            }

            string thirdChar = String.Empty;
            if (s.Length > 2)
            {
                thirdChar = s.Substring(2, 1);
                Regex regexB = new Regex(@"[1-9:/]");
                Match match3 = regexB.Match(thirdChar);
                if (!match3.Success)
                    return false;
            }

            int firstForwardSlash = 0;
            int secondForwardSlash = 0;
            firstForwardSlash = s.IndexOf("/");
            if (firstForwardSlash != -1)
            {
                secondForwardSlash = s.Substring(firstForwardSlash+1,s.Length-firstForwardSlash).IndexOf("/");
            }

            return false;
        }
    }
}

Open in new window

AlHal2Author Commented:
If I enter a long string of letters with no : or / it passes validation.
AlHal2Author Commented:
What about this?

public bool IsValid(String s)
        {
            try
            {
                    string FirstChar = s.Substring(0, 1);
                    Regex regexA = new Regex(@"[a-zA-Z]");
                    Match match1 = regexA.Match(FirstChar);
                    if (!match1.Success)
                        return false;
                
                string secondChar = String.Empty;
                string ThirdChar = String.Empty;
                if (s.Length > 1)
                {
                    secondChar = s.Substring(1, 1);
                    Regex regexB = new Regex(@"[a-zA-Z:/]");
                    Match match2 = regexB.Match(secondChar);
                    if (!match2.Success)
                        return false;
                }
                if (s.Length > 2)
                {
                    ThirdChar = s.Substring(1, 1);
                    Regex regexC = new Regex(@"[a-zA-Z:/]");
                    Match match3 = regexC.Match(secondChar);
                    if (!match3.Success)
                        return false;
                }


                int firstForwardSlash = 0;
                int secondForwardSlash = 0;
                string FirstNumber = "";
                string SecondNumber="";
                int ConvertedFirstNumber;
                int ConvertedSecondNumber;
                firstForwardSlash = s.IndexOf("/");
                if (firstForwardSlash > 0)
                {
                    if (s.Length > firstForwardSlash)
                    {
                        secondForwardSlash = s.IndexOf("/", firstForwardSlash + 1);
                        if (secondForwardSlash > firstForwardSlash + 1 && secondForwardSlash < firstForwardSlash + 4)
                        {
                            FirstNumber = s.Substring(firstForwardSlash+1, secondForwardSlash - firstForwardSlash-1);
                            SecondNumber = s.Substring(secondForwardSlash+1, s.Length - secondForwardSlash-1);
                            if (int.TryParse(FirstNumber, out ConvertedFirstNumber) == true && int.TryParse(SecondNumber, out ConvertedSecondNumber) == true)
                            {
                                if (int.Parse(FirstNumber) < int.Parse(SecondNumber) && int.Parse(SecondNumber) < 13 && int.Parse(FirstNumber) > 0)
                                {
                                    return true;
                                }
                                else
                                {
                                    return false;
                                }
                            }
                            else
                            {
                                return false;
                            }
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    if (s.Length > 3)
                    {
                        return false;
                    }
                }
            }
            catch (SqlException ex)
            {
                lblErrs.Text = lblErrs.Text + "SQL Error. " + ex.Number + ": " + ex.Message;
            }
            catch (Exception ex)
            {
                lblErrs.Text = lblErrs.Text + "Non SQL Error. " + ex.Message;
            }
                return false;
            
        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlHal2Author Commented:
It works - I think
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.