Required Regex for mobile number validation

AnandKumar1
Tera Expert

Hi Team,

i need regex for validating mobile number. Its simple that we need to validate 1 variable when user gives his mobile number it should start with country code (example : +91,+46). If not it should show an alert that your number is not valid as its missing with country code.

 

I have checked in previous community questions but for country code validation i am not able to find. Need assistance. 

 

Thanks.

8 REPLIES 8

MrMuhammad
Giga Sage

Hi Anand,

Please try the below regex. 

/^(\+)([1-9]{2})(\s)(\d{10})$/g

Live test here - Phone Number with country code validation

Hope that helps!

Regards,

Muhammad

Regards,
Muhammad

Ct111
Giga Sage

It is not exact strict but it will help your purpose.....

 

^\+[1-9]{1}[0-9]{3,14}$

AnandKumar1
Tera Expert

I am using below code. But i am getting common alert and even if i give any value or remove 1 number. But when i try to submit with out + sign. it should not allow me to submit. 

Did i miss something here?

 

function onChange(control, oldValue, newValue, isLoading) {
if(isLoading || newValue == ''){
return;
}

var pattern = /^(\+)([1-9]{2})(\s)(\d{10})$/g;
if(!pattern.test(newValue)){
alert('Phone enter a valid phone number');

g_form.setValue('u_number', '');
}}

Where is return false stmt if you want to test it on submit Or And do one thing make this field mandatory so that when you clear value by last step code then it will not allow submit