5

I am having a textbox and i want to count number of occurrence of '.'

if textbox is already having a '.' then user is not allowed to type '.' from his key board

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 index of occurrence of '.' but i want to count occurrences of it.

7 Answers 7

8

You can use the below code to find the number of time a character "." occurs in a string.

    var regex = new RegExp(/\./g)
    var count = "This is some text .".match(regex).length;
Sign up to request clarification or add additional context in comments.

Comments

3

Your regex needs to be changed. "." in regex means everything. You need to escape the ".". Probably like this...

$(this).val().match(/\./g);

Comments

1

You can try this:

$('.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)
   }
});

Number of occurences here is a.length-1.

jsfiddle

1 Comment

How would I go about allowing 1.1.2016 w/o having the two periods removed, yet not allow 1.1..2016 or such duplicate periods?
0

try below javascript functinon on onkeypress or onkeyup event of a tex box

and call like this onkeypress = "return (event,'txtid',9,2)" beforelength is before '.' how many number allowed and afterLength is 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;
}

Comments

0

If you are trying to alert user(or other advanced stuff) and that's why you need the count then you need to add 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 the dots then you can use:

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

Comments

0

This is what you want exactly..:) Demo here

Escape the character and just count it.

Javascript:

 $('.txt').keyup(function() {
            var ele = $(this).val();

                            alert(ele.match(/\./g).length);

        });

HTML

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

Comments

0

I have 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;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.