Wednesday, January 23, 2013

Sanitize Phone no, if phone no length is more than 10 then, Remove 0, +91, 91 from phone no



Sanitize Phone no, if phone no length is more than 10 then, Remove 0, +91, 91 from phone no.
All Indian phone no's length is 10 digits including with STD code



/**
 *  Sanitize Phone no, if phone no length is more than 10 then
 */
function sanitizePhoneNo(phone){
if(phone.length >= 10) {
if(phone[0] == 0) {
phone = makePNo(phone, 1);
}else if( (phone[0] == 9) && (phone[1] == 1) ){
phone = makePNo(phone, 2);
}else if( (phone[0] == '+') && (phone[1] == 9) && (phone[2] == 1) ){
phone = makePNo(phone, 3);
}
}
return phone;
}
function makePNo(val, cnt){
var a='';
for(var i=0; i<=(val.length)-1; i++){ if(i>=cnt){ a += val[i];} }
return a;
}


/**
 *  Validate Phone no with regular expression
 */
function validatePhoneNo(phone){
if( /^[0-9\ \-]+$/.test(phone)){
if(phone.length==10){return true;}else {return false;}
}else {return false;}
}