Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to use same technique on click of a button, but I am facing issues with year greater than current year, Can you please have a look, if I missed something?

C#
private static Regex exprNumber = new Regex("^(405|505)(?19[5-9][0-9]|[2-9][0-9]{3})[0-9]{6}$");

if (txtsfn.Text != "" && txtlrn.Text !="" && txtovrbn.Text != '' && txtovrts.Text != "" && txtfpfn.Text != "" && txtfpln.Text != "" && txtfpfn.Text.Length >= 2 && txtfpln.Text.Length >= 2 &&txtsfn.Text.Length == 13 && txtlrn.Text.Length == 13)
{
    // m.Groups[year].Value will contain the year upon success
    // and be empty if the match fails
    Match m = exprNumber.Match(txtsfn.Text);
    int testedYear = 0;
    int.TryParse(m.Groups[year].Value, out testedYear);
    int currentYear = DateTime.Now.Year;
    
    if (!m.Success || testedYear > currentYear)
    {
        txtsfn.Focus(); // Set focus to the control that should be 
    }
}


It works fine with validating.

Any help is appreciated.

Thanks.
Posted
Updated 16-Nov-15 17:02pm
v3
Comments
Patrice T 16-Nov-15 23:06pm    
You have already posted a question on the same regex.
http://www.codeproject.com/Questions/1055117/Regex-Expression-to-validate-textbox?arn=0
and got answers.
Can you add details on the problem.
[no name] 16-Nov-15 23:16pm    
can you share more details on the your facing issue

1 solution

First you can change the massive if-statement in the beginning as
txtsfn.Text != "" and txtsfn.Text.Length == 13 will be taken care of by the regex.

Then you have forgotten to actually add a group name to the regular expression.
C#
private static Regex exprNumber = new Regex("^(405|505)(?19[5-9][0-9]|[2-9][0-9]{3})[0-9]{6}$");

should be
C#
private static Regex exprNumber = new Regex("^(405|505)(?<year>(19[5-9][0-9]|[2-9][0-9]{3}))[0-9]{6}$");

This means that m.Groups[year].Value will always be empty and as you have no check on that you don't notice this.
As this actually is a design issue and not a run time issue, you could add a debug print out so you at least will see the problem when debugging.
C#
if (txtlrn.Text !="" && txtovrbn.Text != '' && txtovrts.Text != "" && txtfpfn.Text != "" && txtfpln.Text != "" && txtfpfn.Text.Length >= 2 && txtfpln.Text.Length >= 2 && txtlrn.Text.Length == 13)
{
    // m.Groups[year].Value will contain the year upon success
    // and be empty if the match fails
    Match m = exprNumber.Match(txtsfn.Text);
    int testedYear = 0;
    int.TryParse(m.Groups[year].Value, out testedYear);
    if (testedYear < 1950)
        Debug.WriteLine("The year is not correct: {0}", testedYear);

    int currentYear = DateTime.Now.Year;

    if (!m.Success || testedYear > currentYear)
    {
        txtsfn.Focus(); // Set focus to the control that should be
    }
}
 
Share this answer
 
Comments
Member 12076824 17-Nov-15 18:49pm    
Thanks for reply Goerge.

It woks fine with other parts except for checking year over 2015, it accepts all the years till 9999.

Debugger shows year is not correct, but it doesn't give error message I added as:

if (!m.Success || testedYear > CurrentYear)
{


//MessageBox.Show("Please check SFN format");
errorProvider1.SetError(txtsfn, "Please check SFN format");

txtsfn.Focus();
}

Thanks
George Jonsson 17-Nov-15 19:53pm    
Set a break point on this line
if (!m.Success || testedYear > currentYear)
and check the value of testedYear compared to currentYear
Member 12076824 18-Nov-15 12:00pm    
Thanks George, problem was with error-provider. I had to use separate error providers for !m..success and testedyear > current year.

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