[RESOLVED]Jquery auto thousand separator when input

When i click input on textbox, i want jquery auto change number to thousand separator (1234 -> 1,234)

and allow user input number chacracter (not allow A B C # @ ~ ….. and not allow number 0 as first chacracter)

Thanks!

p/s: sorry my english not good

i used plugin jQuery Number Format

http://plugins.jquery.com/df-number-format/

if textbox empty, plugin right

<script src="~/Jquery/jquery.number.min.js"></script>

<input type="text" id="txt1" />

<script>
    $(function () {
        $("#txt1").number(true);
    });
</script>

plugin failed when i selected all text and press 123 -> it show 231

<script src="~/Jquery/jquery.number.min.js"></script>

<input type="text" id="txt1" value="12345" />

<script>
    $(function () {
        $("#txt1").number(true);
    });
</script>

You can try the
Jquery autoNumric plugin
to add separators to your number. This plugin is having an option to remove the leading zeros from textbox.

Sample Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <!-- jQuery and Plugin References -->
  <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  <script src="http://www.decorplanit.com/plugin/autoNumeric-1.9.18.js"></script>
  <script type='text/javascript'>
    $(function($) {
      $('#txtNumber').autoNumeric('init', {  lZero: 'deny', aSep: ',', mDec: 0 });    
    });  
  </script>
</head>
<body>
  <input type="text" id="txtNumber" >
</body>
</html>

thank sir!!!!!!

bug

public ActionResult Index1()
{
       return View();
} 
 
[HttpPost]
public ActionResult Index2(double Price)
{
       return Content(Price.ToString());
}
 
@using (Html.BeginForm("Index2", "Home", FormMethod.Post, new { @class = "ajaxForm" }))
{              
    <input type="text" id="Price" name="Price" class="txtNumber1" />
 
    <button type="submit">Submit</button>
}
 
<script>    
    $(function () {
        var options = {
            target: "",
            success: function (message) {
                alert(message);
            }
        };
 
        $(".ajaxForm").ajaxForm(options);
    });
 
 
    $(function () {
        $(".txtNumber1").autoNumeric("init", { lZero: "deny", aSep: ",", mDec: 0 });
    });
</script>

When i type 1234 (1,234), Price = 1234, my code is right

But it i change double? Price (or Nullable<double) and i type 1234 (1,234) Price alway null

public ActionResult Index2(double? Price)

I mail to bob@decorplanit.com, but he cant understand what i mean

Sorry my english very bad

Thank so much!

Leave a Reply