JSFiddle - React, Tailwind, and code Playground
JavaScript
function IsAustralianTelephoneNumberValid(a_telephone)
{
a_telephone = a_telephone.replace(/\s/g, ''); // remove all spaces
// if is empty OR first char is NOT 0
if((a_telephone=='')||(a_telephone.charAt(0)!='0'))
{
alert("Not a valid phone number");
return false;
}
// lets save the length of that string before we remove digits
length_with_digits = a_telephone.length;
// now string has its digits removed
a_telephone = a_telephone.replace(/0|1|2|3|4|5|6|7|8|9/g,'');
// if is nothing, then there was no other characters in string
// except digits and spaces AND ALSO if the difference of length before the digits
// removal and now is 10 then we can be sure we had 10 digits and nothing else,
// so its valid. Any other case is not valid.
if((a_telephone=='')&&(length_with_digits-a_telephone.length==10))
{
alert('ok');
return true;
}
else
{
alert("Not a valid phone number");
return false;
}
}
// 3 invalids
IsAustralianTelephoneNumberValid('11234567891');
IsAustralianTelephoneNumberValid('012345678999999');
IsAustralianTelephoneNumberValid('11234 dang56789');
// and 3 valids
IsAustralianTelephoneNumberValid('0123456789');
IsAustralianTelephoneNumberValid('01234 56789');
IsAustralianTelephoneNumberValid(' 0123 4 56789');