IValidatableObject not validating length of 3 inputs

190 Views Asked by At

I've failed for quite some time trying to figure this out. I am trying to prompt the user for the area code, suffix, and prefix of a phone number. The phone number is only valid if it's at least 7 digits, but no more than 10. I have tried the IValidatableObject method but i cannot get it to work. Any ideas why that is?

class:

public class Pharmacy:IValidatableObject
    {                           
        public string PhoneNumber
        {
            get
            {
                return _phoneNumber;
            }
            set
            {
                _phoneNumber = value;
            }
        }           
        private string _phoneNumber;
        [Required]
        public string Area
        {
            get
            {
                try
                {
                    return _phoneNumber.Split(new char[] { '(', ')', '-' }, StringSplitOptions.RemoveEmptyEntries)[0].Trim();
                }
                catch
                {
                    return "";
                }
            }
        }
        [Required]
        public string Prefix
        {
            get
            {
                try
                {
                    return _phoneNumber.Split(new char[] { '(', ')', '-' }, StringSplitOptions.RemoveEmptyEntries)[1].Trim();
                }
                catch
                {
                    return "";
                }
            }

        }
        [Required]
        public string Suffix
        {
            get
            {
                try
                {
                    return _phoneNumber.Split(new char[] { '(', ')', '-' }, StringSplitOptions.RemoveEmptyEntries)[2].Trim();
                }
                catch
                {
                    return "";
                }
            }          
        }       

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {                
        var phone = Area.Length + Prefix.Length + Suffix.Length;
        if (phone < 7) 
        {
            yield return new ValidationResult("Phone number must be longer than 7 digits");
        }
        if (phone > 10)
        {
            yield return new ValidationResult("Phone number cannot exceed 10 digits")
        }     
        }      
    }

view:

@using (Html.BeginForm("AddAccount", "RxCard", FormMethod.Post, new { id = "Add", enctype = "multipart/form-data" }))
            {
                @Html.ValidationSummary(true)

                <fieldset>                 
                    <div class="form">                       
                        <label style="margin: 5px" id="lblPhoneNumber">Phone Number (optional)</label>
                        @Html.TextBoxFor(model => model.Pharmacy.Area, new {  @onkeyup = "tabout(this,'Pharmacy_Prefix');", @maxlength = "3", @style = "float:left; width:5em" })
                        @Html.TextBoxFor(model => model.Pharmacy.Prefix, new { @onkeyup = "tabout(this,'Pharmacy_Suffix');", @maxlength = "3", @style = "float:left; width:5em" })
                        @Html.TextBoxFor(model => model.Pharmacy.Suffix, new { @maxlength = "4", @style = "float:left; width:5em" })

                        <input type="hidden" id="IgnoreDuplicate" name="IgnoreDuplicate" /> 
                    </div>
                    <br/>
                </fieldset>
                <button type="submit" value="Save" name="AddNew" id="AddNew" data-toggle="modal">Save</button>
                <button type="submit" value="Cancel">Cancel</button> 
            }               
0

There are 0 best solutions below