【问题标题】:check phone number in a object class检查对象类中的电话号码
【发布时间】:2015-07-25 05:47:29
【问题描述】:

我正在尝试使联系人类代表将出现在我的联系人管理器应用程序中的各个联系人。我需要检查电话号码的格式是否为 123-456-7890,但我不确定如何执行此操作?

代码:

 class Contact
{
    //private member variables
    private String _firstName;
    private String _lastName;
    private Type _contactTypes;
    private String _phoneNumber;
    private String _emailAddress;




    //Public constructor that takes five arguments
    public Contact(String firstName, String lastName, Type contactTypes, String phoneNumber, String emailAddress)
    {
        //Call the appropriate setter (e.g. FirstName) to set the member variable value
        FirstName = firstName;
        LastName = lastName;
        ContactTypes = contactTypes;
        PhoneNumber = phoneNumber;
       EmailAddress = emailAddress;

    }


    /*********************************************************************
     * Public accessors used to get and set private member variable values
     *********************************************************************/
    //Public  ContactTypes accessor
    public Type ContactTypes 
    {
        get
        {
            //Return member variable value
            return _contactTypes;
        }
        set
        {
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("ContactType must have a value");
            else
                //Otherwise set member variable value
                _lastName = value;
        }
    }
    enum ContactTypes { Family, Friend, Professional }
    //Public FirstName accessor: Pascal casing
    public String GetFirstName
    {
        get
        {
            //Return member variable value
            return _firstName;
        }
        set
        {
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("First name must have a value");
            else
                //Otherwise set member variable value
                _firstName = value;
        }
    }

    //Public LastName accessor: Pascal casing
    public String GetLastName
    {
        get
        {
            //Return member variable value
            return _lastName;
        }
        set
        {
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("Last name must have a value");
            else
                //Otherwise set member variable value
                _lastName = value;
        }
    }



    //Public PhoneNumber accessor
    public String GetPhoneNumber
    {
        get
        {
            //Return member variable value
            return _phoneNumber;
        }
        set
        {
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("PhoneNumber must have a value");
            else
                //Otherwise set member variable value
                _lastName = value;
        }
    }



    //Public Email accessor
    public String GetEmailAddress
    {
        get
        {
            //Return member variable value
            return _emailAddress;
        }
        set
        {
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("EmailAddress must have a value");
            else
                //Otherwise set member variable value
                _emailAddress = value;
        }
    }

【问题讨论】:

标签: c#


【解决方案1】:

您可以使用以下正则表达式

bool isValid = Regex.IsMatch(value, @"/d{3}-/d{3}-/d{4}"); 

【讨论】:

    【解决方案2】:

    阅读 C# 中的 regular expressions

    【讨论】:

      【解决方案3】:

      您可以使用下面的文章,其中包含如何使用它的代码示例:

      http://www.authorcode.com/how-to-use-regular-expression-for-validating-phone-numbers-in-net/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-03
        • 2018-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多