Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to validate a mobile no through client side. I have validated it as mobile no should start with country code and it should start with 9,8 or 7. Now i want to validate it so that it should not less than 12 digit including country code. how can i achieve this? can any one suggest me some sample code?
my code looks like this:
JavaScript
var txtPhone = document.getElementById(phoneID); //validating user phone no

     if (txtPhone.value != "") {

         var y = txtPhone.value;
         if (isNaN(y) || y.indexOf(" ") != -1) {
             alert("Invalid Mobile No.");
//             return false;
         }
         if (y.length > 12 || y.length < 12) {
             alert("Mobile No. should be entered with country code");
            // return false;
         }
         if (!(y.charAt(0) == "9" || y.charAt(0) == "8" || y.charAt(0) == "7")) {
             alert("Mobile No. should start with 9,8 or 7 ");
            // return false;
         }

     }
Posted
Updated 19-Feb-13 1:06am
v2
Comments
Karthik Harve 19-Feb-13 7:06am    
[Edit] added pre tags.
Karthik Harve 19-Feb-13 7:08am    
What is the error or problem ?
Ankur\m/ 19-Feb-13 7:35am    
What's wrong with the code you wrote?
ZurdoDev 19-Feb-13 7:52am    
Doesn't your code work? The alternative is to use regular expressions.

1 solution

1. Take an advantage of HTML5 validation
HTML
<input type="tel" name="phone" required=""></input>


http://diveintohtml5.info/forms.html[^]

2. Use the fallback where not supported

http://afarkas.github.com/webshim/demos/index.html[^] or similar

3. Always to server side validation in addition. It is not quite clear what language you use, but any server side language has some validation libraries.
 
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