Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using DAValidation in my asp.net application, I have to apply Numeric validation on contact number, also on the length on contact number which should be minimum 11 digits.
This is how I am applying annotation

C#
[StringLength(20, MinimumLength = 11, ErrorMessage = "Contact number should have minimum 11 digits")]
        [Range(0, Int64.MaxValue, ErrorMessage = "Contact number should not contain characters")]
        public string CONTACT_NUMBER
        {
            get { return m_CONTACT_NUMBER; }
            set
            {
                m_CONTACT_NUMBER=value;
            }
        }


It is working fine,but the problem I am facing is that if I enter abc in contact number field,it will check for length first(ie it gives error message (Contact number should have minimum 11 digits)), However it should validate the datatype incase of non numeric input.
How can I fix this?

What I have tried:

C#
[StringLength(20, MinimumLength = 11, ErrorMessage = "Contact number should have minimum 11 digits")]
        [Range(0, Int64.MaxValue, ErrorMessage = "Contact number should not contain characters")],/pre>
Posted
Updated 11-May-16 3:53am
v3

Add code as below

C#
[DataType(DataType.PhoneNumber)]
    [Display(Name = "CONTACT_NUMBER ")]
    [Required(ErrorMessage = "CONTACT_NUMBER Required!")]
    [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Entered CONTACT_NUMBER format is not valid.")]
    public string CONTACT_NUMBER { get; set; }
 
Share this answer
 
Place the Range Attribute at top
C#
[Range(0, Int64.MaxValue, ErrorMessage = "Contact number should not contain characters")]
       [StringLength(20, MinimumLength = 11, ErrorMessage = "Contact number should have minimum 11 digits")]
       public string CONTACT_NUMBER
       {
 
Share this answer
 

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