Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to validate phone number for mobile application, before submitting form.. how can i?

I have tried the following code,but it allows " 9999.99999","999999999+",(maxlength is 10)..

C#
if (isNaN(mobile))
    // else if(num.test(mobile) == false)
    {
       alert("Invalid phone number");
        $( "#mphone" ).focus();
        $('#mphone').val("");

         return false;
    }



my requirement: it should not allow spaces between number and it should not allow(.,+,- between number). should allow only number 9999999999

Help me please....
Posted
Updated 26-Sep-13 0:06am
v4

You tend to annoy users when they don't know the correct format you want for your mobile no. I only check that the user has entered only digits.

JavaScript
if (! /^\d+$/.test(mobile) ){
//This does not contain digits.
}

//or if you want to check for a certain number of digits

if (! /^\d{8}$/.test(mobile) ){
//This string does not have 8 digits.
}
 
Share this answer
 
 
Share this answer
 
v2
Comments
Brian A Stephens 27-Sep-13 9:18am    
This regex allows any number of digits. He said it should be 10 max. There should obviously be a minimum too, but that was not stated.
Thomas ktg 30-Sep-13 1:14am    
The second link gives in here states the limitation of the phone number. I think this could be used to limit the numbers to maximum or minimum.
Hi Ramkumar,

I hope this link[^] would help you a bit

Try to google out something you will find some more optimized solutions for your requirement.

Regards,
RK
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900