用于验证文本框的正则表达式。 [英] Regex expression to validate textbox.

查看:127
本文介绍了用于验证文本框的正则表达式。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在点击按钮时使用相同的技术,但我面临的问题是年份大于当年,如果我错过了什么,请你看看吗?



  private   static  Regex exprNumber =  new 正则表达式(  ^(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将包含成功的年份
// 如果匹配失败则为空
匹配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(); // 将焦点设置为应该
}
}的控件





验证工作正常。



任何帮助都表示赞赏。



谢谢。

解决方案

);

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将包含成功的年份
// 如果匹配失败则为空
匹配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(); // 将焦点设置为应该
}
}的控件





验证工作正常。



任何帮助都表示赞赏。



谢谢。


首先,您可以将开头的大量if语句更改为

txtsfn。文本!=和txtsfn.Text.Length == 13将由正则表达式处理。



然后你忘了实际添加一个组名到正则表达式。

 私人 静态 Regex exprNumber =  new 正则表达式(  ^(405 | 505 )(19 [5-9] [0-9] | [2-9] [0-9] {3})[0-9] {6} 

);



应该是

  private   static  Regex exprNumber =  new 正则表达式(  ^(405 | 505)(小于?年>(19 [5-9] [0-9] | [2-9] [0-9] {3}))[0-9] {6 } 

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?

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.

解决方案

"); 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.


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.

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


");


should be

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


这篇关于用于验证文本框的正则表达式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆