IValidatableObject未验证3个输入的长度

时间:2016-09-14 21:20:13

标签: c# asp.net-mvc ivalidatableobject

我已经失败了一段时间试图解决这个问题。我试图提示用户输入电话号码的区号,后缀和前缀。电话号码只有在至少7位数但不超过10位时才有效。我已尝试过IValidatableObject方法,但我无法使其工作。任何想法为什么会这样?

类:

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")
        }     
        }      
    }

视图:

@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 个答案:

没有答案