Help with regular expression...

Discussion in 'HTML' started by Noozer, Jun 28, 2005.

  1. Noozer

    Noozer Guest

    Not sure which newgroup would be best... but since this is being used in a
    Javascript function on a web page I guess it could go here...

    I'm looking for some help with a regular expression. Users can enter an
    account number with or without hyphens. Format for an account number is
    000-0000-0000.

    Is there a regular expression I can use to ensure that the hyphens are
    always there?
    User input ->Result
    12312341234 -> 123-1234-1234
    123-12341234 -> 123-1234-1234

    How about to always remove hyphens.
    User input -> Result
    123-1234-1234 -> 12312341234
    1231234-1234 -> 12312341234

    Finally, any regular expression to ensure that only 11 characters have been
    entered and only digits have been entered. What changes would be need to
    accept digits AND hyphens.
    User input -> Result
    123-1234-1234 = False
    12312341234 = True
    1231234ABCD = false
    1231234123 = false
    ...with changes...
    123-1234-1234 = True
     
    Noozer, Jun 28, 2005
    #1
    1. Advertising

  2. On 28/06/2005 11:59, Noozer wrote:

    > Not sure which newgroup would be best... but since this is being used in a
    > Javascript function on a web page I guess it could go here...


    This group takes many subjects, but a scripting question is still a
    scripting question, and I'd have asked in clj.

    [snip]

    > Is there a regular expression I can use to ensure that the hyphens are
    > always there?
    > User input ->Result
    > 12312341234 -> 123-1234-1234
    > 123-12341234 -> 123-1234-1234


    As simply a test:

    /^\d{3}-\d{4}-\d{4}$/.test(accountNumber)

    See below for the conversion form.

    > How about to always remove hyphens.
    > User input -> Result
    > 123-1234-1234 -> 12312341234
    > 1231234-1234 -> 12312341234


    accountNumber = accountNumber.replace(/^(\d{3})-?(\d{4})-?(\d{4})$/,
    '$1$2$3');

    If you inserted hyphens into the string literal, you could do the reverse.

    Note that the hyphens in the regular expression are followed by a
    question mark (?), which indicates that they are optional. The
    assignment statement above would cope with both input forms you presented.

    > Finally, any regular expression to ensure that only 11 characters have been
    > entered and only digits have been entered.


    /^\d{11}$/

    > What changes would be need to accept digits AND hyphens.


    I think the first case would cover that.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Jun 28, 2005
    #2
    1. Advertising

  3. Noozer

    Noozer Guest

    Thanks! It's GREATLY appreciated!

    "Michael Winter" <> wrote in message
    news:mQawe.57954$...
    > On 28/06/2005 11:59, Noozer wrote:
    >
    > > Not sure which newgroup would be best... but since this is being used in

    a
    > > Javascript function on a web page I guess it could go here...

    >
    > This group takes many subjects, but a scripting question is still a
    > scripting question, and I'd have asked in clj.
    >
    > [snip]
    >
    > > Is there a regular expression I can use to ensure that the hyphens are
    > > always there?
    > > User input ->Result
    > > 12312341234 -> 123-1234-1234
    > > 123-12341234 -> 123-1234-1234

    >
    > As simply a test:
    >
    > /^\d{3}-\d{4}-\d{4}$/.test(accountNumber)
    >
    > See below for the conversion form.
    >
    > > How about to always remove hyphens.
    > > User input -> Result
    > > 123-1234-1234 -> 12312341234
    > > 1231234-1234 -> 12312341234

    >
    > accountNumber = accountNumber.replace(/^(\d{3})-?(\d{4})-?(\d{4})$/,
    > '$1$2$3');
    >
    > If you inserted hyphens into the string literal, you could do the reverse.
    >
    > Note that the hyphens in the regular expression are followed by a
    > question mark (?), which indicates that they are optional. The
    > assignment statement above would cope with both input forms you presented.
    >
    > > Finally, any regular expression to ensure that only 11 characters have

    been
    > > entered and only digits have been entered.

    >
    > /^\d{11}$/
    >
    > > What changes would be need to accept digits AND hyphens.

    >
    > I think the first case would cover that.
    >
    > Mike
    >
    > --
    > Michael Winter
    > Replace ".invalid" with ".uk" to reply by e-mail.
     
    Noozer, Jun 28, 2005
    #3
    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. Anand

    Regular Expression help...

    Anand, Jul 9, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,236
    Eric J. Roode
    Jul 9, 2003
  2. Eric B.
    Replies:
    1
    Views:
    435
    Jim Gibson
    Dec 17, 2004
  3. VSK
    Replies:
    2
    Views:
    2,336
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    863
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,998
    vforvikash
    Dec 29, 2008
Loading...

Share This Page