Javascript how to allow period in script that removes everything except numbers? -
here code have period "." allowed entered. code right allows numbers. needs changed allow happen?
function check(o) { v=o.value.replace(/^\s+|\s+$/,''); // remove whitespace if(o=='') { return; } v=v.substr(v.length-1); if(v.match(/\d/g)==null) { o.value=o.value.substr(0,o.value.length-1).replace(/^\s+|\s+$/,''); } };
also see if there more efficient way write code? can optimizations made it? allow numbers , periods entered. in advance.
you want use regex match numbers , or periods: /(\.\d)*/
source: mdn page on string.prototype.match https://developer.mozilla.org/en/docs/web/javascript/reference/global_objects/string/match
Comments
Post a Comment