简体   繁体   中英

JQuery - Adding commas to number inputs but not on submit

I know of a JQuery plugin that adds commas to your number inputs such as JQuery Number Format , but in my testing the data becomes invalid because the numbers have been converted into text and there are two situations I'm trying to overcome:

  1. When the data submits to the DB it's recorded as text because of the comma's placed in the data (MySQL DB using Varchar for the column)
  2. When the data is used in a calculation (either data pulled from DB or function using inputs data not pulled from DB)

Does anyone have any suggestions on how to format numbers with comma's, but the comma's are removed for calculations and on submit? There is nearly 100 places this needs to take into account, so a scalable method is preferable.

You can allow the form to submit the input value even though it still contains the commas, just process it in the server-side to remove the commas:

PHP

$input = $_POST['input'];              // e.g. '1,000,000'
$input = str_replace(',', '', $input); // $input == '1000000'

// other operations (maybe save it to db?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM