0

I am trying to trim strings inside a modal box. I have attempted trimming the strings with regex and shortened available characters in MVC. I am still struggling with getting the intended result i am looking for. Any suggestions would be great! Thank you.

     $("#divUpdateAddress").css("display", "block");
    var p = $.ajax({
        url: '/Portal/ChangeAddress',
        data: { 'type': profileType, policyno: $('#liPolicyNo').html() },
        cache: false,
        contentType: 'application/json; charset=utf-8',
        async: false,
        sucess: function (data) {
            aa($.parseJSON(data));
        },
        error: function (data) {
            debugger;
            alert(data[0]);
        }
    }).responseText;
    $("#divUpdateAddress").html($.parseJSON(p).update_section.html);
    $('#divUpdateAddress').dialog("open");
    $(".ui-dialog-title").html(header);
});
$('#divUpdateAddress').dialog({
    autoOpen: false,
    width: 800,
    resizable: false,
    draggable: true,
    title: 'Update Address',
    modal: true, 
    show: { effect: 'blind' },
    open: function (event, ui) {
    }
});

});

<ol class="row">
        <li class="cell" style="width: 20%;">Phone Number:</li>
        <li class="cell last" style="width: 60%;">
            @Html.TextBoxFor(model => model.PhoneNumber, new { @class = "textbox", maxlength = 15 }) @Html.ValidationMessageFor(model => model.PhoneNumber)
        </li>

   [DataType(DataType.PhoneNumber)]
    [Required(ErrorMessage = "Phone Number Required!")]
    [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})\s+\Z$", ErrorMessage = "Entered phone format is not valid.")]
Leonardo Wildt
  • 2,529
  • 5
  • 27
  • 56
  • Any info on the downvote would be helpful. – Leonardo Wildt Mar 10 '15 at 12:29
  • its unclear what input/output you expect. – Daniel A. White Mar 10 '15 at 12:33
  • Thats fair, ok so the modal box creates a text input box for a phone number, I use regex to validate the phone number and tried to trim the end of the string that way. Since the phone number comes in with white space i need to be able to trim the string in order to eliminate white space and be able to validate. – Leonardo Wildt Mar 10 '15 at 13:20

1 Answers1

1

If you want to trim the string values as they move through the javascript and you are using jQuery you could use something like...

$.trim('  your string   ');

If you are looking to trim all strings before they are entered into the database another solution could be to create a custom model binder.

Here is a an example.

public class TrimModelBinder : DefaultModelBinder
{
    protected override void SetProperty(ControllerContext controllerContext, 
      ModelBindingContext bindingContext, 
      System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)
    {
      if (propertyDescriptor.PropertyType == typeof(string))
      {
        var stringValue = (string)value;
        if (!string.IsNullOrEmpty(stringValue))
          stringValue = stringValue.Trim();

        value = stringValue;
      }

      base.SetProperty(controllerContext, bindingContext, 
                          propertyDescriptor, value);
    }
}

This question shows one way to set that.

Community
  • 1
  • 1
Jonathan Kittell
  • 7,163
  • 15
  • 50
  • 93
  • Any ideas on where that class would go? I havent found a decisive conclusion as to where that would fit in the project. – Leonardo Wildt Mar 10 '15 at 14:28
  • It depends on the structure but I would probably just put in the Models folder. Here is another article that goes more in depth. http://www.codeproject.com/Tips/806415/Model-Binding-using-IModelBinder-and-DefaultModelB – Jonathan Kittell Mar 10 '15 at 14:53