Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I properly set a Regex

currently I am working on an input field and I want to control the given input.

To set input characters I am using a Regex.

I only want to allow all numbers, letter (capital and normal),"-", ".".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My Regex looks like this in C#:

Regex regex = new Regex(@"[\w.-]+$");

Am I overseeing something?

>Solution :

It looks fine, but you only check end of string not start, should add ^ at beginning. In \w also "_" is valid character, which might give you unexpected results, you could replace it with [a-zA-Z0-9]

Regex regex = new Regex(@"^[a-zA-Z0-9.-]+$");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading