Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NETJavascript
how to validate phone and   mobile number in javascript.
            for example phone no -044 27221343. mobile no-9944034466/8428664422  
            /7666544221
Posted 17-Aug-11 19:37pm


3 solutions

Javascript also supports RegExp (regular expressions). I would suggest using Expresso's phone number regexp examples as a base. They have thought about lots of things like area codes in brackets, spaces or dashes between groups of digits, etc. You will need to think about what you want to support:
your local conventions / rules for numbers
domestic area codes
international numbers and dialling prefix
and so on.
 
It's not as simple as some people think.
 
Peter
 
[edit] Here's a simple example of using a Javascript regexp
var pattern = /^\(\d{3}\)\s*\d{3}(?:-|\s*)\d{4}$/
if (pattern.test(string)) {
    // string looks like a good (US) phone number with optional area code, space or dash in the middle
}
[/edit]
  Permalink  
function Validate()
        {
            var x = document.form1.txtPhone.value;
            var y = document.form1.txtMobile.value;
           if(isNaN(x)||x.indexOf(" ")!=-1)
           {
              alert("Enter numeric value")
              return false; 
           }
           if (x.length>8)
           {
                alert("enter 8 characters");
                return false;
           }
           if (x.charAt(0)!="2")
           {
                alert("it should start with 2 ");
                return false
           }
 
           if(isNaN(y)||y.indexOf(" ")!=-1)
           {
              alert("Enter numeric value")
              return false; 
           }
           if (y.length>10)
           {
                alert("enter 10 characters");
                return false;
           }
           if (y.charAt(0)!="9")
           {
                alert("it should start with 9 ");
                return false
           }
        }
 
  Permalink  
hi,,
 

<script type="text/javascript">
    function onlyNumbers(event) {
        var e = event || evt; // for trans-browser compatibility
        var charCode = e.which || e.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
 
        return true;
    }
</script>
 

 
this will allow only numbers in text
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 6,959
1 Prasad_Kulkarni 3,689
2 OriginalGriff 3,402
3 _Amy 3,332
4 CPallini 2,950


Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 8 Apr 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid