Regular expression for checking a string field with specific criteria

I am wondering for a regular expression that only accepts letters, numbers and dashes.

I will add it to my viewmodel class. I am working on . Using MVC data annotations.

I want to check one row field there. With this regex.

+4
source share
3 answers

Regex, which matches only letters, numbers, and dashes:

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

We recommend that you try the gskinner regex tool , which is very useful.

+1
source

enter image description here

If you pay attention to the screenshot above. Above is the case passed, and below is the failed case.

+2
source

you can use this

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

This will confirm all alphabets, numbers and dashes.

0
source

Source: https://habr.com/ru/post/1497540/


All Articles