Regex for Türkiye mobile number: +90, 05xx and different spellings
Normalize +90, 0, and space phone entries first; Verify the format of the Türkiye mobile number with a flexible and easy-to-maintain regex.
Türkiye mobile phone numbers by user+90 5xx, 05xx, can be written with spaces, parentheses or hyphens. It's easier to normalize the input first, then validate the national number format, rather than resolving all these options into a single, hard-to-read regex.
The following approach checks the format of the number; It does not prove that the line is actually allocated, active or remains owned by a specific operator. Due to number portability, it is also not reliable to derive current operator results from prefix.
Specify target format
Let's store the normalized mobile number in 10-digit national format without leading country and trunk code:
5XXXXXXXXX
Basic regex for this format:
^5\d{9}$
^And$matching the entire value,5mobile phone area,\d{9}represents the remaining nine digits. Pinning certain operator prefixes into the regex may cause valid numbers to be rejected when BTK allocations are updated.
If the field is not required, handle the null value separately in the form rule instead of validating it with the phone regex. In this way, the rule "can be left blank" does not interfere with the rule "if it is written, it must be in the correct format".
Normalize and validate with JavaScript
function normalizeTrMobile(input) {
let digits = String(input).replace(/\D/g, '');
if (digits.startsWith('0090') && digits.length === 14) {
digits = digits.slice(4);
} else if (digits.startsWith('90') && digits.length === 12) {
digits = digits.slice(2);
} else if (digits.startsWith('0') && digits.length === 11) {
digits = digits.slice(1);
}
return /^5\d{9}$/.test(digits) ? digits : null;
}
normalizeTrMobile('+90 (532) 123 45 67'); // "5321234567"
normalizeTrMobile('0532 123 45 67'); // "5321234567"
normalizeTrMobile('532-123-45-67'); // "5321234567"
When the function succeeds it returns a uniform format, when it failsnullgives. You can keep the country code separate in the database or prefix the validated value for E.164 presentation.+90you can add
Know the limit of free character clearing
replace(/\D/g, '')is user friendly; Removes parentheses and spaces. However, it can also combine numbers within random text. If a strict form is required, first check the allowed characters:
const allowedInput = /^\+?[0-9()\s-]+$/;
function validatePhoneField(value) {
if (!allowedInput.test(value.trim())) return null;
return normalizeTrMobile(value);
}
Provide a clear error message in the interface for invisible characters that come with copy-paste. Showing the accepted sample format instead of “Phone number is invalid” guides the user faster.
Why isn't Regex validation enough?
Regex only validates the length and starting digit. A separate ownership check, such as sending a single-use code, is required to verify that the number belongs to the user. Code duration, number of attempts, resend limit and abuse protection should be implemented in SMS verification.
If the operator or number type is a strictly required business rule, an up-to-date, authoritative numbering source should be used. Do not write all the prefixes that seem valid today into Regex and assume that they will not change for years.
Frontend and backend must share the same rule
HTMLpatternor JavaScript control improves user experience; can be easily skipped. The backend should reapply the same normalization and validation rule. Instead of logging the erroneous value as is, mask it according to your personal data policy and retain it only for as long as necessary.
Short guide
Common mistake
Compressing all the +90, 0, space and parentheses options into a single complex regex or thinking that operator prefixes are permanent.