Allowing user to type only one "."
Posted
by
Tartar
on Stack Overflow
See other posts from Stack Overflow
or by Tartar
Published on 2013-11-06T21:11:29Z
Indexed on
2013/11/06
21:54 UTC
Read the original article
Hit count: 92
I am trying to implement a simple javascript-html calculator. What i want to do is,typing only one '.' by the user. How can i control this ? Here is the code that i tried.
I can already find the number of '.' but i'am confused now also this replaceAll function is not replacing '.' with empty string.
String.prototype.replaceAll = function(search, replace)
{
//if replace is null, return original string otherwise it will
//replace search string with 'undefined'.
if(!replace)
return this;
return this.replace(new RegExp('[' + search + ']', 'g'), replace);
};
function calculate(){
var value = document.calculator.text.value;
var valueArray = value.split("");
var arrayLenght = valueArray.length;
var character = ".";
var charCount = 0;
for(i=0;i<arrayLenght;i++){
if (valueArray[i]===character) {
charCount += 1;
}
}
if(charCount>1){
var newValue=value.replaceAll(".","");
alert(newValue);
}
}
© Stack Overflow or respective owner