Reputation: 153
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>
Upvotes: 1
Views: 2676
Reputation: 2597
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.
Upvotes: 0
Reputation: 644
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}
Upvotes: 1
Reputation: 441
A simple case:
<script type="text/javascript">
var reg = /^\d{10}$/;
function PhoneValidation(phoneNumber)
{
return reg.test(phoneNumber);
}
</script>
Upvotes: 0
Reputation: 432
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
}
Upvotes: 0