I had a field on a web page where a person would fill in their tax rate as a percentage. Of course, people would put in the percent sign ( % ) along with the number and it would choke the web page. So I needed a quick javascript function to strip out unwanted characters, and leave just numbers and the period if it were a decimal number.
Here is the snippet required. I take the form input. The form name is refForm and taxRate is an input value. The ^ is a negation sign in javascript so I am taking everything out except numbers and decimal.
var si = document.refForm.taxRate.value.replace(/[^\d.]/g, "");
document.refForm.taxRate.value = si;
I assign the original value to a variable and then pass the value back to taxRate after the characters are gone.
Easy way to replace all characters and just leave the numbers using javascript.
No comments:
Post a Comment