1

I have an MVC Application that asks the user for the Phone Number. There is a mask so it always looks like "(567) 222-0001.

When this phone number is saved in the database, it gets saved with these brackets and the hyphen. Instead, I would like to save it to the DB without the brackets and the hyphen i.e. a raw phone number. The application uses LINQ.

Any help is appreciated. Also, I am new to MVC, so I have a very rough idea where to look in the code.

TylerH
  • 20,799
  • 66
  • 75
  • 101
dsingh23
  • 185
  • 3
  • 14

1 Answers1

6
Regex.Replace(phoneNumber, @"[^0-9]", "")

There's not really a "before it's saved to the DB" place to tie into, at least not easily. I've seen other frameworks such as RoR or Django that do let you do something like define a "pre_save" method or similar that provides a hook, but Entity Framework has no similar functionality.

However, you can do it in other ways. Likely the easiest is to define a custom setter on the property:

private string phoneNumber;
public string PhoneNumber
{
    get { return phoneNumber; }
    set { phoneNumber = !string.IsNullOrWhiteSpace(value) ? Regex.Replace(value, @"[^0-9]", "") : null; }
}

Then, every time some value is set for that property, it will automatically remove any non-numeric characters. Alternatively, you can also just clean it up in the action before saving:

model.PhoneNumber = Regex.Replace(model.PhoneNumber, @"[^0-9]", "");
db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Thanks! I mentioned in my question that I am new to MVC and I have a rough idea where to work around with this thing in the code. – dsingh23 Sep 02 '15 at 18:27
  • I need to know the place before it gets stored in the DB. Because, that's my intention, to store the phone number in a variable, use Regex.Replace(phoneNumber, @"[(|)|-]", ""); and then it gets stored in the DB. – dsingh23 Sep 02 '15 at 18:29
  • @dsingh23: That extra twist does may your question more unique. If you change your question to make it more obvious what your ultimate goal is, I can reopen the question. – Chris Pratt Sep 02 '15 at 18:31
  • Wouldn't it make more sense to access the Raw text (Value) property of the masked textbox? We'd need to know more about control/mechanism the OP is using to mask, but it seems much more appropriate than RegEx's. – Lynn Crumbling Sep 02 '15 at 18:40
  • @LynnCrumbling: a (good) JS masking plugin wouldn't output any of the mask characters. If that's what's going on, then, yes, I would suggest the OP either configure the plugin to not include the mask characters, or find another plugin if such an option doesn't exist. However, assuming that it's being posted as-is, a regex is the best way to remove it after the fact. – Chris Pratt Sep 02 '15 at 18:42
  • I'm guessing that the OP just isn't aware of the fact that they can get the data without the masking. – Lynn Crumbling Sep 02 '15 at 18:43
  • @dsingh23 What plugin are you using? – Lynn Crumbling Sep 02 '15 at 18:44
  • @LynnCrumbling: Well, some people *do* actually type dashes and such when entering phone numbers manually, and you can't rely solely on JS to restrict input. JS can be disabled or may not even be supported, so you still always need a fallback to cleanse and sanitize your data server-side. – Chris Pratt Sep 02 '15 at 18:46
  • True...I'll bow out :) – Lynn Crumbling Sep 02 '15 at 18:48
  • @LynnCrumblin: The application is not using a plugin. I have a configuration.cs file where the phone number is added to the fields and where the input mask, pattern have been defined. 'var phone = new StringField {Id = ++fieldId, Name = "Phone", DefaultLabel = "Phone No", Pattern = @"^\(\d{3}\) \d{3}-\d{4}$", InputMask = "(999) 999-9999", FieldCategory = FieldCategory.FormField };context.StringFields.Add(phone1); fields.Add(phone);' – dsingh23 Sep 02 '15 at 19:01