PaRsH
PaRsH

Reputation: 1320

Regular expression to validate the user input

I want a regular expression, which should start with alphabet or numbers and followed by alphanumeric character and in between, it may or may not contain forward or backward slash (\,/).

Highly appreciate your help.

Thanks :)

Upvotes: 0

Views: 84

Answers (3)

effone
effone

Reputation: 2192

^[a-zA-Z0-9]+[a-zA-Z0-9\/\\]*

This will accept / or \ only in between.

Edit: Oops:p Missed the numbers, updated. Thanks for pointing out.

Upvotes: 0

NeverHopeless
NeverHopeless

Reputation: 11233

Try this regex:

(?i)^[a-z\d][a-z\d\\/]*$

?i treats upper-case letters as lower-case letters.

Upvotes: 0

Gaurang Tandon
Gaurang Tandon

Reputation: 6753

/^[a-z0-9][\w\\\/]+$/i
  1. Start of line (^).
  2. [a-z0-9] - a letter or number - 1 occurrence
  3. [\w\\\/]+ - multiple occurrences of alphanumeric characters (including _) or \ or /.
  4. Line end ($).

ignore-case flag will accept both uppercase and lowercase.

[xyz] specifies a character class meaning that either x or y or z can be matched.

DEMO

If you don't consider 123_asd to be alpha-numeric, use:

/^[a-z0-9][a-z0-9\\\/]+$/i

Hope it helps!

Upvotes: 1

Related Questions