Simple Javascript Problem

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

    Simple Javascript Problem

    Hello,

    I have a simple problem which I am guessing has an easy answer within
    JavaScript.

    I have a form1 with two fields, field1 and field2. I want the contents of
    field1 to be transferred to field2 onBlur, but with some changes.

    I want the contents of field1 to have its spaces replaced with Dashes, and
    all punctuation removed, so this can happen:

    FIELD 1 value:
    Today's rate is 15%, & the outlook is good.

    onBlur should then create a value of:

    FIELD 2 value:
    todays-rate-is-15-percent-and-the-outlook-is-good

    Notice that the "&" was converted to "and", the "%" to "percent". The comma
    was deleted and all spaces replaced with Dashes. The second field should
    only every contain alphabetic letter, dashes, and numbers.

    Any ideas guys?

    Cheers,

    Jim.


  • Randy Webb

    #2
    Re: Simple Javascript Problem

    Jim wrote:
    [color=blue]
    > Hello,
    >
    > I have a simple problem which I am guessing has an easy answer within
    > JavaScript.
    >
    > I have a form1 with two fields, field1 and field2. I want the contents of
    > field1 to be transferred to field2 onBlur, but with some changes.[/color]

    onChange would be a better event handler to use instead of onBlur.
    [color=blue]
    > I want the contents of field1 to have its spaces replaced with Dashes, and
    > all punctuation removed, so this can happen:
    > FIELD 1 value:
    > Today's rate is 15%, & the outlook is good.
    >
    > onBlur should then create a value of:
    >
    > FIELD 2 value:
    > todays-rate-is-15-percent-and-the-outlook-is-good
    >
    > Notice that the "&" was converted to "and", the "%" to "percent". The comma
    > was deleted and all spaces replaced with Dashes. The second field should
    > only every contain alphabetic letter, dashes, and numbers.[/color]

    Create a simple object or array that has all of the possible % & ^ @
    that you want converted to words. Then you do a RegExp replace on them.
    Then you create a second table of entries for puntuation that you want
    replaced. Then you loop through the string and replace puntuation. Then
    you replace spaces with Dashes.

    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

    Comment

    • Jim

      #3
      Re: Simple Javascript Problem

      > Create a simple object or array that has all of the possible % & ^ @ that[color=blue]
      > you want converted to words. Then you do a RegExp replace on them. Then
      > you create a second table of entries for puntuation that you want
      > replaced. Then you loop through the string and replace puntuation. Then
      > you replace spaces with Dashes.[/color]


      OK..... :)
      Can you provide a really simple example? I haven't really got a clue where
      to start (here? :) )

      Jim.


      Comment

      • Randy Webb

        #4
        Re: Simple Javascript Problem

        Jim wrote:
        [color=blue][color=green]
        >>Create a simple object or array that has all of the possible % & ^ @ that
        >>you want converted to words. Then you do a RegExp replace on them. Then
        >>you create a second table of entries for puntuation that you want
        >>replaced. Then you loop through the string and replace puntuation. Then
        >>you replace spaces with Dashes.[/color]
        >
        >
        >
        > OK..... :)
        > Can you provide a really simple example?[/color]

        Start by posting a list of punctuation you want converted, along with
        what they should be converted to. Then add a list of punctuation you
        want removed. Then consider this sentence:

        Should the period in 123.27 be removed or left alone? It is not as
        simple a prospect as you might think it is.

        Also, should it's be changed to it is or to its?

        --
        Randy
        comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

        Comment

        Working...