0

I need to check if the last character of an input (type="number") value is a dot!

Tried these, but it doesn't work:

let amount = $("#amount").val();
let last_char = amount.slice(-1);
if(last_char == ".")
{
    return;
}

Also tried amount[amount.length-1] and charAt()

P.S. Input type is type="number"

Edit:

None of the answers work: https://jsfiddle.net/dne37hao/

Community
  • 1
  • 1
J. Doe
  • 812
  • 1
  • 15
  • 33
  • What's the use case here? You could use regex to detect the period, look here for a succinct answer: https://stackoverflow.com/questions/32657021/check-if-string-begins-with-punctuation-javascript – Nathaniel Flick Sep 27 '19 at 04:41
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith – Durga Sep 27 '19 at 04:47
  • Possible duplicate of [endsWith in JavaScript](https://stackoverflow.com/questions/280634/endswith-in-javascript) – User863 Sep 27 '19 at 05:03
  • @User863 It is not a duplicate: https://jsfiddle.net/dne37hao/ – J. Doe Sep 27 '19 at 05:11
  • @J.Doe Technically `3.` is equal to `3` https://stackoverflow.com/questions/18852244/how-to-get-the-raw-value-an-input-type-number-field – User863 Sep 27 '19 at 05:32
  • 1
    The problem here isn't the matching for the last character. The problem is that, whilst reading the field value with a dot in the end its being interpreted as an empty field. So that's why your checks for the last character are not matching. This question will help you https://stackoverflow.com/questions/18677323/html5-input-type-number-value-is-empty-in-webkit-if-has-spaces-or-non-numeric-ch – noa-dev Sep 27 '19 at 05:57

3 Answers3

3

const string = 'Is this a question.';

console.log(string.endsWith('.'));
1

You can achieve this using different ways but with different performance,

1. Using bracket notation:

var str = "Test";
var lastLetter = str[str.length - 1];

But it's not recommended to use brackets.

2. charAt[index]:

var lastLetter = str.charAt(str.length - 1)

This is readable and fastest among others. It is most recommended way.

3. substring:

str.substring(str.length - 1);

4. slice:

if(str.slice(-1)=== '.'){
return true
}

It's slightly faster than substring.The most efficient one

With ES6:

You can use str.endsWith("t");

Abdul Basit Mangat
  • 1,112
  • 9
  • 18
-2

You can try this:

let amount = $("#amount").val();
if(amount[amount.length-1] === "."){
   return true;
}
Dino
  • 7,779
  • 12
  • 46
  • 85
virender nehra
  • 470
  • 6
  • 12
  • what value are you getting in amount variable any example ? – virender nehra Sep 27 '19 at 04:58
  • For example: `1.` `36.` it doesn't skip these. input type is also number – J. Doe Sep 27 '19 at 05:01
  • So jquery .val() method return string, javascript convert number to string and ignore the . in the end. so if you print amount in console it never show dot(.) in the end, change input type as text then you can check if input has a dot in the end or not. – virender nehra Sep 27 '19 at 05:06
  • Number can not end with dot in javascript, javascript basically ignore . in the end of the number – virender nehra Sep 27 '19 at 05:19