How to calculate the number of characters in a string using jquery

I have a text box and I want to count the number of occurrences of '.'

if there is already a '.' in the text box then the user is not allowed to enter ".". from his keyboard

here is my code:

$('.txt').keyup(function() { var ele = $(this).val(); var contains = (ele.indexOf('.') > -1); if (contains) { var count = $(this).val().match(/./g); if (count > 1) { var cont = $(this).val(); var str = $(this).val().length; $(this).val(cont.substring(0, str)); } } }); 

$ (this) .val (). match (/./g) gives me the index of occurrence of '.' but I want to count it.

+4
source share
7 answers

You can use the code below to find the number of characters of time "." going on in line.

  var regex = new RegExp(/\./g) var count = "This is some text .".match(regex).length; 
+7
source

Your regular expression needs to be changed. "in regex means everything. You need to avoid." ". Probably like this ...

 $(this).val().match(/\./g); 
+3
source

You can try the following:

 $('.txt').keyup(function() { var str = this.value; var a = str.split('.'); if (a.length > 2) { this.value = a.slice(0, 2).join('.');//removes 2nd dot and the string following it //a[0] +='.'; this.value = a.join('');//only removes redundant dots (alternative) } }); 

The number of occurrences here is a.length-1 .

jsfiddle

+1
source

This is what you want for sure .. :) Demo here

Reset the character and just rate it.

JavaScript:

  $('.txt').keyup(function() { var ele = $(this).val(); alert(ele.match(/\./g).length); }); 

HTML

 <textarea id="txt"></textarea> 
0
source

try below javascript functinon on onkeypress or tex window onkeyup event

and call onkeypress = "return (event,'txtid',9,2)" beforelength before '.' how many valid numbers and afterLength for after '.' number length

 function isNumberKey(event, obj, beforeLength, afterLength) { var keyCode1 = event.keyCode; var keyCode = 0; if (keyCode1 == 0) keyCode = event.which; else { keyCode = keyCode1; } // alert(keyCode); // alert(keyCode1); if ((keyCode >= 48 && keyCode <= 57) || keyCode == 46 || keyCode == 13 || keyCode == 27 || keyCode == 127) { var text = document.getElementById(obj).value; if (keyCode == 46 && keyCode1 == 0) { if (text.toString().indexOf(".") != -1) { return false; } } if (keyCode == 46) { if (text.toString().indexOf(".") != -1) { return false; } } // if (!/^\d{0,10}(?:\.\d{0,2})?$/.test(text)) { // return false; // } else { // } var splitText = text.split('.'); if (splitText[0].length >= beforeLength) { if (keyCode == 46 && text.toString().indexOf(".") == -1) { return true; } else if (text.toString().indexOf(".") != -1) { return true; } return false; } // if (splitText.length > 1 && splitText[1].length == afterLength) { // return false; // } } else { return GetDefault(event); } return true; } function GetDefault(event) { var keyCode = event.keyCode; if (keyCode == 0) keyCode = event.which; if (keyCode == 8 || keyCode == 9 || keyCode == 35 || keyCode == 36 || keyCode == 37 || keyCode == 38 || keyCode == 39 || keyCode == 40 || keyCode == 46 || keyCode == 118) { return true; } return false; } 
0
source

If you are trying to warn the user (or other advanced data) and why you need an account, you need to add the length:

 $('.txt').keyup(function() { var ele = $(this).val(); var i = ele.indexOf('.'); if (i > -1) { var c = ele.match(/\./g).length); // ... } }); 

If you are just trying to delete points, you can use:

 $('.txt').keyup(function() { $(this).val($(this).val().replace('.', '')); }); 
0
source

I found my way ....

 var count = 0; $('.txt').blur(function () { var t = $(this).val(); if (t.indexOf('.') >= 0) { var j = t.split('.'); count = j.length - 1; } else { count = -1; } } 
0
source

Source: https://habr.com/ru/post/1490429/


All Articles