471,594 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Format textbox value using regexpression

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:
Jul 20 '05 #1
3 9363
Unfortunately, I hit send before I was ready. Here is the rest of my post:

I found a script that does commas using Regular expresssions:
http://javascript.internet.com/forms/commas.html

If I knew the right regular expression for my situation, I could modify this
code to work for me. Can anyone help?

thank you

"H Branyan" <hb****************@merc.mercer.edu> wrote in message
news:bn************@ID-92475.news.uni-berlin.de...
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:

Jul 20 '05 #2
"H Branyan" <hb****************@merc.mercer.edu> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
This info should be enough for me to get what I want done. I appreciate
your quick and thorough response.
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:k7**********@hotpop.com...
"H Branyan" <hb****************@merc.mercer.edu> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Assimalyst | last post: by
11 posts views Thread by RipperT | last post: by
9 posts views Thread by Mark G. | last post: by
7 posts views Thread by pamela fluente | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.