Hi I would like to know how can I count number of period in Input or textbox using jQuery or Javascript. For Example my value in textbox is "120.0.0."
Asked
Active
Viewed 494 times
-2
-
2We don't do your homework for you. Give it a try. If you can't get it to work, post what you have done and what problems you are having and we will try to help you. – Robert Columbia Aug 04 '16 at 03:36
-
2Probably duplicate of http://stackoverflow.com/questions/881085/count-the-number-of-occurences-of-a-character-in-a-string-in-javascript – Girdhari Agrawal Aug 04 '16 at 03:37
4 Answers
2
Use this statement:
var length = ("120.0.0.".match(/\./g)).length
You can replace 120.0.0 with your your input field value

Girdhari Agrawal
- 375
- 3
- 18
1
RegEx
var temp = "127.0.0.1";
var count = (temp.match(/[.]/g) || []).length;
console.log(count);

DDan
- 8,068
- 5
- 33
- 52
1
Use the val()
function and then loop through each of the characters in the string incrementing the count if that character is a period.
var textBoxVal = $('#myTextBox').val();
int periodCount = 0;
for (var i = 0; i < textBoxVal.length; i++) {
if (textBoxVal[i] === '.') {
periodCount++;
}
}

Sumner Evans
- 8,951
- 5
- 30
- 47
1
$('#btn').click(function() {
var temp = $('#test').val();
var count = (temp.match(/\./g) || []).length;
console.log(count);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id='test' placeholder='Enter the text here.'/>
<button id='btn'>Check</button>

Ayan
- 2,300
- 1
- 13
- 28