Click here to Skip to main content
11,429,154 members (63,600 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
how to validate phone and   mobile number in javascript.
            for example phone no -044 27221343. mobile no-9944034466/8428664422  
            /7666544221
Posted 17-Aug-11 20:37pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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)



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | Last Updated 19 Jun 2014
Copyright © CodeProject, 1999-2015
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