Click here to Skip to main content
12,504,695 members (56,176 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Javascript
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:
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 19-Feb-13 1:03am
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?
ryanb31 19-Feb-13 7:52am
   
Doesn't your code work? The alternative is to use regular expressions.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

1. Take an advantage of HTML5 validation
<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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100