Format textbox value using regexpression

Discussion in 'Javascript' started by H Branyan, Oct 22, 2003.

  1. H Branyan

    H Branyan Guest

    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:
    H Branyan, Oct 22, 2003
    #1
    1. Advertising

  2. H Branyan

    H Branyan Guest

    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" <> wrote in message
    news:bn67a2$n7tk9$-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:
    >
    >
    H Branyan, Oct 22, 2003
    #2
    1. Advertising

  3. "H Branyan" <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Oct 22, 2003
    #3
  4. H Branyan

    H Branyan Guest

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


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

    <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    > 'Faith without judgement merely degrades the spirit divine.'
    H Branyan, Oct 22, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim in Arizona

    RegExpression Help

    Jim in Arizona, Apr 2, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    285
    Jim in Arizona
    Apr 2, 2007
  2. AGSHAR
    Replies:
    1
    Views:
    583
    AGSHAR
    May 31, 2008
  3. ruca

    Format textbox to time format

    ruca, May 6, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    821
    HJohns
    May 9, 2005
  4. ErwinP
    Replies:
    1
    Views:
    721
    ErwinP
    Aug 19, 2005
  5. kazaam
    Replies:
    4
    Views:
    84
    Robert Klemme
    Oct 1, 2007
Loading...

Share This Page