简体   繁体   中英

Regex for mobile number with special characters in c#

I'm trying to build a regular expression to validate mobile numbers that accepts () , - and + . I am totally new to this and this is what I could build

\+?\(?([0-9])\)?[- ]?\(?([0-9])\)?

But I know this wrong since it is accepting data such as 123456- which is not right. Can somebody please help me build a regex so that itdoes not accept inputs like

123)3575
12349-
-2345678

There is no limit to the number of digits entered. I'm expecting it to accept inputs of type

(123)4567-67898
+234567890
1234-4567-67

Please help..

Something like

/^(\+|\(\d+\))?\d+(-\d+)*$/

Regex Demo

Test

Regex.IsMatch("(123)4567-67898", @"^(\+|\(\d+\))?\d+(-\d+)*$");
=> True

Regex.IsMatch("-2345678", @"^(\+|\(\d+\))?\d+(-\d+)*$");
=> False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM