nam nam - 1 month ago 4
jQuery Question

Jquery not removing commas and dollar symbols from number

In my ASP.NET MVC Core project, in the following

I'm using jquery to try to remove $ symbol and commas from the SaleAmount input tag before the form is submitted. But it's not doing anything. When I put alert('Test') inside the myJSFunction(), the alert successfully pops up that means the function is called but it's code for removing commas and $ is not working.


public class SaleViewModel
[DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = true)]
public float? SaleAmount { get; set; }


public async Task<IActionResult> UpdateSales(List<SaleViewModel> model, string returnUrl = null)


@model IList<MyProj.Models.SaleViewModel>
<form id="target" asp-controller="DbRelated" asp-action="UpdateSales" asp-route-returnurl="@ViewData[" ReturnUrl"]" method="post" onsubmit="myJsFunction()">
<table class="table">
@for (int i = 0; i < Model.Count(); i++)
<td>@Html.EditorFor(r => r[i].SaleAmount, new { htmlAttributes = new { @class = "saleAmt" } })</td>
<button type="submit" class="btn btn-default">Save</button>

@section scripts
$(function () {
$("#target").submit(function (event) {
function myJsFunction() {
parseFloat($('.saleAmt').val().replace(/,/g, '').replace(/$/g, ''));
return false;


Your code does not update the values of your textboxes with the modified text. And in any case, your cancelling the submit so nothing will be posted. You can use

$("#target").submit(function () {
    var textboxes = $('.saleAmt');
    $.each(textboxes, function(index, item) {
        var text = $(this).val().replace(/[$,]/g, ''); // remove characters
        $(this).val(text); // update value

Side note, there is no need to use parseFloat()