Pawan
Pawan

Reputation: 2218

Regular expression to validate string field with certain criteria

I am wondering for Regular Expression that accepts only letters,numbers and dashes.

I will add it on my viewmodel class.I am working on application.And using MVC Data Annotations.

I want to validate one string field there.With that regex.

Upvotes: 4

Views: 3375

Answers (3)

Chamaququm
Chamaququm

Reputation: 6728

enter image description here

If you pay attention to the above screen shot. Above is a passed case and below one is failed case.

Upvotes: 2

ubiquibacon
ubiquibacon

Reputation: 10667

Regex that matches only letters, numbers, and dashes:

^[0-9a-zA-Z-]*$

Recommend you try the gskinner regex tool, it is very helpful.

Upvotes: 1

dirtydexter
dirtydexter

Reputation: 1073

you can use this

/[a-zA-Z\d-]+/

this will validate all the alphabets, numbers and dashes.

Upvotes: 0

Related Questions