Format textbox value using regexpression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • H Branyan

    #1

    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:


  • H Branyan

    #2
    Re: Format textbox value using regexpression

    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:
    Making the AI Internet a reality for the world


    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" <hbranyan.SPAMB LOCK@merc.merce r.edu> wrote in message
    news:bn67a2$n7t k9$1@ID-92475.news.uni-berlin.de...[color=blue]
    > 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[/color]
    event,[color=blue]
    > to look like this:
    >
    > XXXX-XX-XXX-XXXX
    >
    > I found a script that does commas using Regular expresssions:
    >
    >[/color]


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Format textbox value using regexpression

      "H Branyan" <hbranyan.SPAMB LOCK@merc.merce r.edu> writes:
      [color=blue]
      > 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,[/color]

      Why not the "onchange" event?
      [color=blue]
      > to look like this:
      >
      > XXXX-XX-XXX-XXXX[/color]

      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.
      [color=blue]
      > I found a script that does commas using Regular expresssions:[/color]

      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.r eplace(/[^\w]+/g,"");

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • H Branyan

        #4
        Re: Format textbox value using regexpression

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


        "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
        news:k76x18y9.f sf@hotpop.com.. .[color=blue]
        > "H Branyan" <hbranyan.SPAMB LOCK@merc.merce r.edu> writes:
        >[color=green]
        > > 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[/color][/color]
        event,[color=blue]
        >
        > Why not the "onchange" event?
        >[color=green]
        > > to look like this:
        > >
        > > XXXX-XX-XXX-XXXX[/color]
        >
        > 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.
        >[color=green]
        > > I found a script that does commas using Regular expresssions:[/color]
        >
        > 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.r eplace(/[^\w]+/g,"");
        >
        > /L
        > --
        > Lasse Reichstein Nielsen - lrn@hotpop.com
        > DHTML Death Colors:[/color]
        <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>[color=blue]
        > 'Faith without judgement merely degrades the spirit divine.'[/color]


        Comment

        Working...