H Branyan said:
I need to format a textbox value a user enters. The user will enter 13
characters, and then I have to format them, preferably in the onBlur
event,
Why not the "onchange" event?
to look like this:
XXXX-XX-XXX-XXXX
How is the text input? If it is just 13 characters, then it is fairly
easy. If the user inputs the charaters with "-"'s in between, you
might need to remove them first. What characters are legal? If only
letters and digits, you can remove all other punctuation first. You
should expect the string to contain hyphens, if the user changes it
after you have formatted it once.
I found a script that does commas using Regular expresssions:
I wouldn't bother with regular expressions when you know where to
split the string.
If there are just 13 characters in the input element, you can do like
this:
---
var str = element.value;
str = str.substr(0,4) + "-" + str.substr(4,2) + "-"
str.substr(6,3) + "-" + str.substr(9,4);
element.value = str;
---
If you need to remove non-alphanumeric characters first, then you
can use a regular expression. Change the first line to
var str = element.value.replace(/[^\w]+/g,"");
/L