Skip to content

Instantly share code, notes, and snippets.

@bendasvadim
Forked from krasnikov87/sum_input
Created May 31, 2018 13:34
Show Gist options
  • Save bendasvadim/89ac1ee6a1ec0d03734d03731be14a99 to your computer and use it in GitHub Desktop.
Save bendasvadim/89ac1ee6a1ec0d03734d03731be14a99 to your computer and use it in GitHub Desktop.
check sum input
this.value = sum(this.value);
function sum(str) {
str=str.trim().replace(/[^\d\.]+/gi,"");
var s=str.indexOf(".",str.indexOf("."));
if(s>=0){
str=str.substr(0,s+3);
var whole = str.substr(0,s);
var fractional = str.substr(s+1, s+3).replace(/[^\d]+/gi,"");
str = whole +'.'+ fractional;
}
return str
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment