1

I have an application form in php.. I need to validate the phone number.. Please tel me how to validate a phone no. Here is the code..

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

var XIPhone= document.forms["XIForm"]["XIPhone"].value;
    if (XIPhone==null || XIPhone=="") { alert("Please Enter Office Phone No"); return false; }

document.getElementById("XIForm").submit();
        }); 


<div class="formItem">
            <label>Office Phone No</label>
            <input type="text" name="XIPhone" id="XIPhone" />
        </div>

    <div class="formItem">
            <input type="hidden" name="formType" id="formType" value="XI" />
            <input type="button" name="XISubmit" id="XISubmit" value="Submit"          class="formButton" />
        </div>

        </form>

    </div>
4

4 Answers 4

1

Here are some regular expression formats :

Phone Number (Format: +99(99)9999-9999) - [\+]\d{2}[\(]\d{2}[\)]\d{4}[\-]\d{4}  
UK Phone Number - ^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$  
USA Phone Number -  US based Phone Number in the format of: 123-456-7890 - \d{3}[\-]\d{3}[\-]\d{4}
Sign up to request clarification or add additional context in comments.

Comments

0
var data = yourdata;
var re = /^\d{10}$/;    //user regular expression (this example is used to check 
                        // validation for 10 numbers or digits).
var a = data.match(re);
if(a == null)
{

   //show error message

}
else
{
   //no error
}
  • Use regular expression
  • Format of Phone numbers and landline numbers will change based on country

Comments

0

A simple case:

<script type="text/javascript">  
      var reg = /^\d{10}$/;  
      function PhoneValidation(phoneNumber)
      {  
        return reg.test(phoneNumber);
      }  
</script>

Comments

0

Try this:

function checkForNumber(number, size) {
    var nbrString = '';
    for (var i = 0; i < number.length; i++) {
        var cc = number.charCodeAt(i);
        if (cc >= 48 && cc < 58) {
            nbrString += number[i];
        }
    }
    return nbrString.length === size;
}

Usage:

var paramA = "012 345 67 89";
var paramB = 10;
checkForNumber(paramA, paramB);

paramA: your number to check in string format

paramB: the size of the number digits (normally it's 10)

You can modify the return parameter in the 'checkForNumber' function to fit your needs.

Here is an example on JSFiddle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.