Reputation: 2080
I have this static method here:
public static string ParseStringForSpecialChars(string stringToParse)
{
const string regexItem = "[^a-zA-Z0-9 ]";
string stringToReturn = Regex.Replace(stringToParse, regexItem, @"\$%&'");
return stringToReturn;
}
This method works fine as it stops many strings from being harmful to my application. I suppose there's a better way to do things, but that's not the point of my post.
Right now what I want is to get all values from any textboxes in a javascript that would call this method and then send the data to the controller.
Say that I have this view:
@using MyApp.Utilities
@model List<MyApp.Models.CustomerObj>
@{
ViewBag.Title = "Customer Index";
}
<h2>Customer Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm())
{
<p>
Name: @Html.TextBox("customerName") <br/>
Address: @Html.TextBox("customerAddress") City: @Html.TextBox("customerCity") <br/>
<br />
Postal Code: @Html.TextBox("customerPC")<br/>
Phone Number: @Html.TextBox("customerPhone") Fax Number: @Html.TextBox("customerFax")<br />
Mail: @Html.TextBox("customerEmail") <br/>
<input type="submit" value="Filter" style="float:right">
</p>
}
</p>
How could I proceed to do what I want to do? Can anyone offers me suggestion how to proceed? I would like a flexible workaround since the view you have right now will change and I would need this method to be appliable everywhere.
Thank you.
Upvotes: 0
Views: 321
Reputation: 1483
Have you considered adding a Data Annotation to your model like this:
[RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
This was captured from: http://www.asp.net/mvc/tutorials/older-versions/models-%28data%29/validation-with-the-data-annotation-validators-cs
Of course, the downside to this is that you would have to add it to all properties in your model that you want it to apply to. You might be able to use remote validation and fire it off via javascript. Otherwise, you will be stuck with doing it on the client side. (But that's not that difficult to do anyway.)
Upvotes: 1