2

I want that a text field should allow float numbers with single dot and If I am copying from somewhere it will replace it and make it a float number.

but below code is working only for number

$("#max_width_value").bind("change keyup input", function (e) {
    if (this.value.match(/\D+/g)) {
        this.value = this.value.replace(/\D+/g, '');
    }
});

Allowed: 0.2, 2.2 , 2.0 , .2

Not Allowed: 2.2. , 0.2.

Text field should not allow more than single dot but if user is copying and pasting in text field then it should paste only floating number or number.

e.g

2.2. becomes 2.2

2.2.2.2.2.2 will also become 2.2

  1. will become 2
dixit thareja
  • 83
  • 1
  • 11
  • Please kindly edit your post to better explain what it is that you are trying to achieve. Providing a simple example would be best, e.g. of a successful match and what it would be like after replacement. – Tacocat Feb 15 '16 at 10:05
  • I am restricting the user to enter a non-floating number in text field – dixit thareja Feb 15 '16 at 10:06
  • Doesn't it work as expected? You remove all non-digit symbols with `.replace(/\D+/g, '')`. With this, you cannot type `2.2.2.2.2.2` – Wiktor Stribiżew Feb 15 '16 at 10:28
  • yess but I want it to type 0.2 , 2.2 but not 0.2. , 2.2. – dixit thareja Feb 15 '16 at 10:32
  • 1
    Possible duplicate of [regular expression for floating point numbers](http://stackoverflow.com/questions/12643009/regular-expression-for-floating-point-numbers) – tripleee Feb 15 '16 at 11:17
  • 1
    This is not a duplicate of [regular expression for floating point numbers](http://stackoverflow.com/questions/12643009/regular-expression-for-floating-point-numbers). Surely, there is such a question, it will take time to find it. – Wiktor Stribiżew Feb 15 '16 at 11:33
  • Check out [this question](http://stackoverflow.com/questions/891696/jquery-what-is-the-best-way-to-restrict-number-only-input-for-textboxes-all/27539768#27539768) and [this fiddle](http://jsfiddle.net/f1w38jw0/10/) - I believe that solution will work as you need. – Wiktor Stribiżew Feb 15 '16 at 12:04
  • Yeahh this fiddle is covering half of my result as If i am pasting 12.1.1.1 , it is not allowing me to enter this no because it is invalid but in my case I want to extract it and make an output as 12.1 – dixit thareja Feb 15 '16 at 13:41

2 Answers2

0

Your first requirement can be translated to the regex

^\d*\.\d+$

The second is a replacement of

^(\d*(\.\d+)?).*$

with

$1
  • Good to know is that you make the point and what follows optional by wrapping it in parenthesis follow by a question mark.

  • We anchor the regex with ^ and $ to make sure its the only input and nothing follows or proceeds ot.

buckley
  • 13,690
  • 3
  • 53
  • 61
  • Thanks ....but I had tried this and it is allowing me to enter alphabets and special symbols as well. – dixit thareja Feb 15 '16 at 10:59
  • The regex above disalows it as it only validates for points and digits. Can you post the input with alphabet symbols that makes the regex match also? – buckley Feb 15 '16 at 12:53
  • Input : abc12.1.1"", This is my input string, firstly textfield allowing me to enter this symbols and alphabets, secondly it is not replacing. As if i am entering the above input I want the output as 12.1 because this is the valid thing for my text field as I am allowing numerics or float value. – dixit thareja Feb 15 '16 at 13:38
0

This could help you:

/^(\d*(?:\.\d+)?)/

^       // anchor for start of string
(       // start of capture group, indicates start of string of interest
\d*     // allows zero or more digits before point
(?:\.   // first point
\d+)?   // allows one or more digits after point optionally
)       // end of capture group

You can get the capture string using $1

I have (?:\d+)? on my regex to handle 2. cases.

Demo


EDIT: If you want to accept only .'s and digits after the string of interest you can use. It will reject any string that contains any character that is not inside []

^(\d*(?:\.\d+)?)[\.\d]*$

I only added

[\.\d]*$ 

it only accepts .'s and digits until the end of the string,

Demo


EDIT: Below snippet show my regex in action. I have used your code as base. I believe that you can adapt to your need.

$(document).ready(function() {
 var value = [$("#valid").html(), $("#invalid").html()]
 var regex = /^(\d*(?:\.\d+)?)[\.\d]*$/
 for(var i=0; i < value.length; i++) {
  if (value[i].match(regex)) { 
   var result = value[i]+" is valid - "
   result += "Replace:" + value[i].replace(regex, '$1') + "\n";
   $("#result_valid").append(result)
  } else {
   var result = value[i]+" is invalid\n"
            $("#result_invalid").append(result)
  }

 }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=valid>12.1.1</div>
<div id=result_valid></div>
<div id=invalid>abc12.1.1</div>
<div id=result_invalid></div>

EDIT: Regarding OP's comments and needs to evaluate string for each char entered in "realtime", I have to change the regex to a more permissive one

/^(\d*\.?\d*)[\.\d]*$/

var regex = /^(\d*\.?\d*)[\.\d]*$/
 $("#spot_size_value").bind("change keyup input", function (e) {
  if (this.value.match(regex)) {
    this.value = this.value.replace(regex, '$1');
  } else{
    this.value = ""
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="col-md-2 control-label" for="spot_size">Spot Size</label>
<div class="col-md-6">
 <input id="spot_size_value" name="spot_size_value" type="text" placeholder="" class="form-control input-sm">
 <div class="validation_error" id="error_spot_size"></div>
</div>
emartinelli
  • 1,019
  • 15
  • 28
  • Thanks for the reply...As it is allowing me to enter alphabets but replacment is working fine if I am pasting 2.2.2.2.2 so it would be 2.2 as you had suggested. ^(\d*(?:\.\d+)?)[\.\d]*$ using this regex – dixit thareja Feb 15 '16 at 13:26
  • On my second demo link, you can check that strings that are not digits nor dots are not allowed. How are you checking it in your code? – emartinelli Feb 15 '16 at 13:45
  • if (this.value.match(/^(\d*(?:\.\d+)?)[\.\d]*$/gm)) { this.value = this.value.replace(/^(\d*(?:\.\d+)?)[\.\d]*$/gm, '$1'); $("#error_spot_size").html(""); } – dixit thareja Feb 15 '16 at 13:50
  • Please, verify the snippet in my answer. – emartinelli Feb 15 '16 at 15:38
  • https://jsfiddle.net/tharejadixit/4mvva8n1/ Please check out my scenario as I had implemented your code snippet according to my requirements .....but its not working properly because it is allowing me to enter special symbols and initially it doesnt allow me to enter dot but whenever I add another special symbol or character then i am able to enter dot. – dixit thareja Feb 16 '16 at 10:26
  • Actually, your problem is not the regex itself, it is the `onBind` event. Your event is evaluating the input before it is a valid string for regex. [See](https://jsfiddle.net/m3gvedv3/), I have changed your event to click, so it can evaluate only when you have a complete string. Maybe you should think in other approach to solve this. – emartinelli Feb 16 '16 at 12:00
  • I completely understand your point..actually my requirement is whenever I am getting value on text field as well as when I am entering..I have to check value so that whenever the value gets pasted from my data source into text field, I have to check that value and if it is not valid i have to convert that value into a valid value e.g if from my data source it is coming 12.1.1.1 I have to change it into 12.1 or 12111 whatever and same in case when I am entering...!!!!! – dixit thareja Feb 16 '16 at 13:13
  • I see. Verify if it helps: https://jsfiddle.net/jqo1zpgk/. Anyway, I believe that it is a problem in the flow of your snippet. – emartinelli Feb 16 '16 at 13:22
  • somehow at some percentage result is coming....but please check we are able to enter special characters and alphabets... – dixit thareja Feb 16 '16 at 13:25
  • It shows an error message for invalid characters. If want to not show invalid characters just clear your input on `else` clause. – emartinelli Feb 16 '16 at 13:28
  • It has solved my problem almost....but when I am copying and pasting from somewhere then it is working unexpectedly e.g paste 12.1.1.1.1 so it would be 12.1 which is fine , but if you paste 12.1.1.1.1.1""" so it would be 12.1.1.1.1.1 not 12.1 or 1211111 – dixit thareja Feb 16 '16 at 15:03
  • True. So, you may just clean the input as it is in the (again) updated answer or use another approach what you want. Anyway, I believe that is no more related to original post. – emartinelli Feb 16 '16 at 15:20
  • 1
    Okay will clear the input as you are suggesting...thanks for the answer !! – dixit thareja Feb 16 '16 at 15:23
  • I was trying the same but I need 15 reputations atleast to thumbs up that this answer is useful but i can make sure that when I will get 15 reputationns then I will do the same for sure !!!! – dixit thareja Feb 16 '16 at 15:40