Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Format textbox value using regexpression

Reply
Thread Tools

Format textbox value using regexpression

 
 
H Branyan
Guest
Posts: n/a
 
      10-22-2003
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:


 
Reply With Quote
 
 
 
 
H Branyan
Guest
Posts: n/a
 
      10-22-2003
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" <(E-Mail Removed)> wrote in message
news:bn67a2$n7tk9$(E-Mail Removed)-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:
>
>



 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      10-22-2003
"H Branyan" <(E-Mail Removed)> 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
H Branyan
Guest
Posts: n/a
 
      10-22-2003
This info should be enough for me to get what I want done. I appreciate
your quick and thorough response.


"Lasse Reichstein Nielsen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "H Branyan" <(E-Mail Removed)> 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 - (E-Mail Removed)
> DHTML Death Colors:

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass value from activex textbox to webform textbox AGSHAR ASP .Net 1 05-31-2008 12:00 PM
regexpression: scanning a special charachters // ? kazaam Ruby 4 10-01-2007 01:19 PM
RegExpression Help Jim in Arizona ASP .Net 5 04-02-2007 08:44 PM
ASP.NET textbox loses text value in composite control but Textbox subclass doesn't!? ErwinP ASP .Net Web Controls 1 08-19-2005 01:50 PM
Format textbox to time format ruca ASP .Net Web Controls 1 05-09-2005 05:09 PM



Advertisments