Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove comma in the input text form using JS before it will be submitted to the server?

Tags:

javascript

I have a form that when submitted to the server will contain commas in the query string values, for example:

 test.com/?manufacturer=0&body-style=0&min-price=270%2C000&max-price=780%2C000

Is there a way to remove this comma using JS just after the user clicks the form but before it will be submitted to the server? I have this approach here: How replace query string values using jQuery?

But that will only change the text to integer but in the URL itself. I also tried using input number attribute in HTML 5 but I cannot figure out how to use a comma by default to show it on the form: How to force to display a number with a comma with input type number?

I would appreciate any tips. Thanks.

UPDATE

Final working solution:

$('.my_form').submit(function() {

var x=$('#input_text_field_one').val();
x=x.replace(/,/g,'');
x=parseInt(x,10);
$('#input_text_field_one').val(x);

return true;

});

input_text_field_one is the input text form containing a number with commas. .my_form is the class name for the form. After form submit the above code replace the numbers with commas to without commas. So finally, the numbers are submitted as number. :)

like image 572
Emerson Maningo Avatar asked Jun 27 '13 11:06

Emerson Maningo


1 Answers

I think you want something like this:-

form.onSubmit = function(){
   form.action = form.action.replace(",", "");
   return true;
}
like image 121
pvnarula Avatar answered Oct 31 '22 20:10

pvnarula