【问题标题】:ASP.NET Custom Validate EmailASP.NET 自定义验证电子邮件
【发布时间】:2015-06-21 11:31:04
【问题描述】:

对于我的学校考试,我们必须验证电子邮件地址,但不允许使用正则表达式验证器。我正在尝试使用自定义验证器解决此问题,但我无法弄清楚。 (电子邮件输入在文本框 (tb_email) 中。

我正在尝试这样解决它:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (tb_email.Text ==  \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* )
        {
            args.IsValid = true;
        } 
        else
        {
            args.IsValid = false;
        }
    }

或类似的东西浮现在脑海中:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (tb_email.Text != "")
        {
            string filter = \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*; 
            args.IsValid = true;
        } 
        else
        {
            args.IsValid = false;
        }
    }

我遇到了很多错误,我只是不知道如何做到这一点。我做了很多搜索,但我只找到了这样的答案:只需使用 Java 中的正则表达式验证器或代码解决方案(我还没有学过)。 我真的很感激一个好的答案!谢谢

【问题讨论】:

    标签: c# asp.net validation customvalidator custom-validators


    【解决方案1】:

    可以在没有 REgularExpressionValidator 的情况下使用正则表达式吗?如果是的话:

    using System;
    using System.Text.RegularExpressions;
    
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (tb_email.Text != "")
        {
            string filter = \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*; 
    
           Regex regex = new Regex(filter );
       Match match = regex.Match(tb_email.Text );
        if (match.Success)
        {
             args.IsValid=true; }
            else
            {
             args.IsValid = false;  
    
      } 
    }
    

    在客户端(javascript)看到这篇文章:

    Validating email addresses using jQuery and regex

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 2013-10-25
      • 2014-04-07
      • 2020-02-23
      • 2020-10-20
      相关资源
      最近更新 更多