Phone Number Validation For (SMS) mTAN
Goals
- Any* phone number is accepted
- Any* format is accepted
- Swiss phone numbers may be entered without international code *Special numbers (e.g. 144, 0900...) must be excluded
Client-side scripting
- If no international code was entered, +41 is automatically and visibly prefixed, and an obsolete - formerly leading - zero is removed.
- Digits in parantheses are automatically removed.
- If the first and second character post trim is 0, these are converted to +.
- Afterwards all non-numercis are removed, except single spaces.
Example client-side scripting
Enter a phone number. Also try copy&paste, e. g. +41 (0) 31-555 00 11 Code
×
<script>
function v(phone) {
phone=phone.replace(/\(+.*?\)+/g,' ');
phone=phone.replace(/[^0-9\ /+]/g,' ');
phone=phone.replace(/^\s+/g, '');
if(phone.substr(0,2)=='00')phone='+'+phone.substr(2,phone.length);
if(phone.substr(0,1)>0)phone='+'+phone;
if(phone.substr(1,1)>0&&phone.substr(0,1)!='+'&&phone.substr(1,2)!='')phone='+41'+phone.substr(1,phone.length);
phone=phone.replace(/ +/g,' ');
phone=phone.replace(/^\s+/g,'');
phone=phone.replace(/[^0-9\ +]/g,'');
if(phone.replace(/\s/g,'').indexOf('+410')===0)phone='+41'+phone.replace(/\s/g,'').substr(4,phone.replace(/\s/g,'').length);
if(phone.split('+').length>2)phone='+'+phone.split('+')[1];
if(phone.substr(0,1)=='+'&&phone.substr(1,1)===0)phone='';
return phone;
}
</script>
Further, the form field HTML tag for the phone number must contain the following:
onchange="if(v(this.value)!=this.value)this.value=v(this.value)" onkeyup="this.onchange()" onfocus="this.onchange()" onblur="this.onchange()" onmouseover="this.onchange()" onmouseout="this.onchange()"
Additional server-side scripting (recommended)
- Remove whitespace
- Is numeric
- Check if intl. prefix is present and existing
- Blacklist (regexes), e. g. 144, 0900...