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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -