On Feb 12, 10:22 am, "J. mp" <joaomiguel.pere... / gmail.com> wrote:
> Gavin Kistner wrote:
> > A regexp for this, allowing multiple dotted names joined together:
>
> > # Regexp for a single name
> > /[a-z\d](?:[a-z\d-]*[a-z\d])?/i
>
> > # Regexp for 1 or more of those joined by periods
> > /(?:[a-z\d](?:[a-z\d-]*[a-z\d])?)(?:\.[a-z\d](?:[a-z\d-]*[a-z\d])?)*/i
>
> So, Gavin your last regex allows only valid host names on an URI? I'm
> sorry for not reading the RFC before. My requirement is what I said, the
> user name will act as part of an URI, so I should allow any combination
> of chars that are valid for the first part of an URI

I think so. I haven't tested it. Actually, I see one minor mistake -
to be safe, anchor this regexp to the start/end to ensure you're
matching exactly what the user entered:
/\A(?:[a-z\d](?:[a-z\d-]*[a-z\d])?)(?:\.[a-z\d](?:[a-z\d-]*[a-z\d])?)*
\Z/i

To be clear, it will match:
f
9
274
3cats7
a.b
a-b
foo
foo-bar
foo.bar
foo.bar.jim
foo-bar.jim-jam
spoofy.com.edu.gov.com.com
crazy.long.name.because.the.regexp.has.no.limits.on.it.whatsoever.for.length

And it will reject:
-foo
foo-
foo.
.foo
foo_bar