Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate phone number

Status
Not open for further replies.

tektipsismyfavorite

Technical User
May 4, 2006
57
US
I am working on a phone number validator and have an input that has maxChars = 12.

The format i'm going for is 999.999.9999

There's also a part that validates if it's a numeral.

I've tried this:(called by onkeypress="return doPhone(this.value, event, 0)")
Code:
function doPhone(currv, evt, decimal){
	evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) {
        return false;
    }
    if (charCode == 46) { 
        if (decimal == 0) {
            return false;
        } else {
         if (currv.indexOf(".") != -1) {
             return false;
         }
     }
 } else if (currv.indexOf(".") != -1) {  
     var s = currv.substring(currv.indexOf("."));
  if (s.length > decimal) {
   return false;
  }
 }
	if (charCode == 10){
		var num = document.moreinfo.cell.value;
		var len = num.length - 1;
		if(document.moreinfo.cell.value.length == 4 || document.moreinfo.cell.value.length == 8){
			document.moreinfo.cell.value = num.substring(0,len);
		}
		return true;
	} else {
		if(document.moreinfo.cell.value.length == 3 || document.moreinfo.cell.value.length == 7){
			document.moreinfo.cell.value += ".";
		}
		return true;
	}
}

The problem i'm having is after the last ELSE right before the last return true.

For some reason if I enter 4 characters, it puts the "." between the 3rd and 4th character, like it should, but then it won't let me type any more numbers. it's like the field ends after it has 5 characters in it. What's goin on?
 
<html>
<head>
<script lang="javaScript">
function doPhone(currv, evt, decimal){
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
((evt.which) ? evt.which : 0));
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) {
return false;
}
if (charCode == 46) {
if (decimal == 0) {
return false;
} else {
// if (currv.indexOf(".") != -1) {
// return false;
// }
}
}
if (charCode == 10){
var num = document.moreinfo.cell.value;
var len = num.length - 1;
if(document.moreinfo.cell.value.length == 4 || document.moreinfo.cell.value.length == 8){
//alert("a");
document.moreinfo.cell.value = num.substring(0,len);
}
return true;
} else {
if(document.moreinfo.cell.value.length == 3 || document.moreinfo.cell.value.length == 7){
//alert("b");
document.moreinfo.cell.value += ".";
}
return true;
}
}
</script>
</head>
<body>
<form name="moreinfo">
<input type="text" name="cell" onkeypress="return doPhone(this.value, event, 0)">
</form>
</body>
</html>
 
something about that wasn't working, but i played with it, and removing those lines did help. This is what I ended up with:
Code:
function doPhone(currv, evt, decimal){
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) {
        return false;
    }
    if (charCode == 46) {
        if (decimal == 0) {
            return false;
        }
 	}
	if((document.moreinfo.cell.value.length == 3 || document.moreinfo.cell.value.length == 7) && (charCode != 10 && charCode != 8)){
		document.moreinfo.cell.value += ".";
	}
	if(charCode == 10 || charCode == 8){
		var num = document.moreinfo.cell.value;
		var len = num.length;
		len = len - 1;
		if(document.moreinfo.cell.value.length == 5 || document.moreinfo.cell.value.length == 9){
			document.moreinfo.cell.value = num.substring(0,len);
		}
	}
	return true;
}

This should be customizable to any phone number format, if i'm not mistaken. anyway, thanks a lot for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top