0

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> 
            }               
thatdude
  • 77
  • 1
  • 2
  • 10
  • A bit unclear what your trying to do here. Why not just a `RegularExpressionAttribute` to limit it to digits only and between 7 and 10 characters. And your `Area`, `Prefix` and `Suffix` properties have `getters` only so there is no point including `TextBoxFor()` in the view for them (they cannot be **set**) –  Sep 14 '16 at 21:52
  • At one point the phone number was displayed in a single textbox but then i was asked to take it and display it in parts instead (UX suggestions). That's when i added "Area", "Prefix", and "Suffix" and assigned them parts of that string. What i need to do now is validate the number of a new account so that someone can't just entere a value in one textbox and leave the other 2 blank. Activating validation using a single @html.validationmessagefor for 3 separate fields is what i do not undestand how to do. – thatdude Sep 14 '16 at 23:20
  • 1
    If you want 3 textboxes, then your `Area`, `Prefix` and `Suffix` need to be simple `public string Area { get; set; }` etc (and decorated with `[Required]` and a `[RegularExpression]` (to limit the input to digits and certain number of charaters), and the `PhoneNumber` just has a getter that combines the other values. And of course you need `@Html.ValidationMessageFor()` for each property. –  Sep 14 '16 at 23:28
  • 1
    You do not need `IValidatableObject` (you class needs a total of 4 lines of code). And I have no idea what you think you doing with the `Split()` code or what you attempting to do with your `onkeyup` - both of which are unnecessary. –  Sep 14 '16 at 23:28

0 Answers0