Reputation: 586
i am trying to find specific text inside textbox using jquery or javascript but it`s not working on chrome
Senerio: i want to add .0 after string but if .0 or .5 etc already exist then do not add .0 after string
$("#ctl00_ContentPlaceHolder1_txtquantity").blur(function() {
var stringvalue = $("#ctl00_ContentPlaceHolder1_txtquantity").val();
if (!stringvalue.contains('.0')) {
var result = stringvalue + ".0";
stringvalue = result;
$("#ctl00_ContentPlaceHolder1_txtquantity").val(stringvalue);
}
});
Upvotes: 0
Views: 1005
Reputation: 10849
It is not a cross browser solution yet to use String.prototype.contains
.
You can use a regular expression though to check if it fits the end of your string
Replace
if (!stringvalue.contains('.0')) {
By
if (!stringvalue.match(/\.[0-9]+$/)) {
Also even though it were cross browser, String.prototype.contains
wouldn't do what you want as you have to cover the cases for any digit sequence behind the mantisse. It would require you to loop through each of the 10 use cases (from 0 to 9) as contains
only accepts a search string and an optional start position.
An even weirder use case is if your number only ends with "."
which is a parsable float in JavaScript...
In this case you could use a greedy regex to do this:
if (!stringvalue.match(/\.[0-9]*$/)) {
Upvotes: 3