Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > HTML > Help with regular expression...

Reply
Thread Tools

Help with regular expression...

 
 
Noozer
Guest
Posts: n/a
 
      06-28-2005
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



 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      06-28-2005
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.
 
Reply With Quote
 
 
 
 
Noozer
Guest
Posts: n/a
 
      06-28-2005
Thanks! It's GREATLY appreciated!

"Michael Winter" <(E-Mail Removed)> wrote in message
news:mQawe.57954$(E-Mail Removed). uk...
> 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.



 
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
using regular phone as voip device with regular modem? hygum VOIP 5 03-23-2005 03:51 PM
Help with a Perl Regular Expression Eric B. Perl 1 12-17-2004 10:56 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM
Regular Expression help... Anand Perl 1 07-09-2003 10:14 AM
Re: Help with regular expressions. Chris R. Timmons ASP .Net 0 07-04-2003 10:49 PM



Advertisments