I have an application to send TAN to users via SMS. We have already API to send SMS to a mobile phone number. Therefore, I have to make sure it's correct mobile phone number. Below is my regex:
function validateMobile(str) {
var filter = /^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$/;
if (!filter.test(str)) {
alert('Please provide a valid mobile phone number');
return false;
}
return true;
}
It doesn't accept characters, only number and '+' allowed. Users can enter, for example +49176xxxxxxxx or 0176xxxxxxxx (no particular country code)
But this regex is seriously flawed. Users can enter whatever numbers, e.g. 1324567982548, this regex also returns true. I thought about to check the length of the textbox, it'd work, for the time being, but still it's not a proper solution.
Is there any other better regex or way to check more concrete a mobilbe phone number?
Thanks in advance.
SOLVED
I solved this with a new regex:
var filter = /^(\+49\d{8,18}|0\d{9,18})((,\+49\d{8,18})|(,0\d{9,18}))*$/;
or as mzmm56 suggested below:
var filter = /^(?:(\+|00)(49)?)?0?1\d{2,3}\d{8}$/;
Both are equally fine.