Format textbox value using regexpression

H

H Branyan

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,
to look like this:

XXXX-XX-XXX-XXXX

I found a script that does commas using Regular expresssions:
 
L

Lasse Reichstein Nielsen

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
 
H

H Branyan

This info should be enough for me to get what I want done. I appreciate
your quick and thorough response.


Lasse Reichstein Nielsen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top