0

I have a student form. I need to validate for Phone number. How to validate it using javascript. My form takes even characters for the phone no. Here is the code. Please tel me where do i need to make changes

$(function() {
            $("#XISubmit").click(function(){

    var XIStudentPhone = document.forms["XIForm"]["XIStudentPhone"].value;

        if (XIStudentPhone==null || XIStudentPhone=="") { alert("Please Enter Student Phone no"); return false; }
            document.getElementById("XIForm").submit();
            });
user3286350
  • 89
  • 2
  • 3
  • 12
  • 1
    It is not easy to validate a phone number since phone numbers can be written in many different ways according to countries and other factors. I think you need to decide one or two allowed formats an use them. – Mir Feb 13 '14 at 10:25
  • use regex. refer this link http://stackoverflow.com/questions/7756510/phone-number-format-in-javascript – anurupr Feb 13 '14 at 10:25

5 Answers5

1

Judging by the designation "Student Phone Number", I'm going to assume that internationalisation is not an issue here.

In such a case, you can do something like this:

<label>Phone number:
    <input type="text" name="XIStudentPhone" pattern="\d{10}" />
    (Please enter numbers only, no spaces or dashes)
</label>

This would allow a 10-digit phone number, such as 555-012-3456, and provides the clear instruction to not include any symbols (therefore requiring entry like 5550123456). Depending on what country you're actually in, you may need to adjust it - for example in England you might have 10 or 11 digit phone numbers, so you would have pattern="\d{10,11}".

It is important to note, however, that this makes an assumption. It assumes that you are only relevant to one particular country, with one particular phone number length. For more general applications, you should probably do something like pattern="\d{5,14}" for a wider range of support.

As always, browser-side validation should not be trusted, so make sure you validate on the server too!

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Try this,

function phonenumber(inputtxt)  
{  

 var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
 if((inputtxt.value.match(phoneno))  
    {  
  return true;  
    }  
  else  
    {  
    alert("message");  
    return false;  
    }  

}

This code checks the phone number in the format xxx-xxx-xxxx

To get other formats refer, http://www.w3resource.com/javascript/form/phone-no-validation.php

Vinod VT
  • 6,946
  • 11
  • 51
  • 75
0
function Validate()
        {
            var x = document.XIForm.XIStudentPhone.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
           }

        }

simple function to validate the phone number. you can modify as per your restrictions required.

TKV
  • 2,533
  • 11
  • 43
  • 56
0

You can try the following code

  XIStudentPhone = XIStudentPhone.replace(/\D/g,'');
  if(XIStudentPhone.length==0) {
        alert(XIStudentPhone + ' contains empty');
     }
    else if (XIStudentPhone.length == 10) {
        alert(XIStudentPhone + ' contains 10 digits');
     }
     else {
        alert(XIStudentPhone + ' does not contain 10 digits');
     }
Sathiyaraj
  • 343
  • 3
  • 11
0

You can use the new input type in html5.

<input type='tel' pattern='[\+]\d{2}[\(]\d{2}[\)]\d{4}[\-]\d{4}' title='Phone Number (Format: +99(99)9999-9999)'>

You can modify the pattern as your wish.

Here is the jsFiddle http://jsfiddle.net/shyamchandranmec/aLMwq/

and here is a valid phone no. as per the example

+99(99)9999-9999
Konza
  • 2,143
  • 17
  • 30