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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Phone number validation

Status
Not open for further replies.

ptuck

MIS
Aug 8, 2003
130
US
I am pulling in a phone number from a database that has multiple formats (ex. (123)555-1234, 1235551234, 123/555/1234). I want to remove any thing from this field that is not a number. Is this possible???

Thanks for the help,
Paul
 
Code:
	function validPhone(input){
		phone=input.value
		ansStr = ""

		for (n=0;n<phone.length;n++)	{
			if (isNum(phone.charAt(n))){
				ansStr = ansStr + phone.charAt(n)
			}
		}

		if(ansStr.length==0){
			input.value=ansStr
			return false
		}

		if (ansStr.length<10 || ansStr.length>10){
			alert ("Please enter a valid 10 digit phone number")
			input.focus()
			input.select()
			return false
		}
		ansStr = "(" + ansStr.substr(0,3) + ") " + ansStr.substr(3,3) + "-" + ansStr.substr(6,4)
		input.value=ansStr
	}

	function isNum(inData){
		if (inData >= 0 && inData <= 9 && inData != " "){
			return true
		}
		return false
	}

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Since you posted this in the Javascript forum I'll assume that you want to get rid of the 'junk' on the client's side and not on the server side.
I'll also assume you only have 10 digit phone numbers. Any international numbers with more than 10 digits won't be recognized. It will need more tweaking on your side if you want to validate those numbers.

Try this.
Code:
<html>
  <body>
    <script>
      function isPhone(objPhone){
       var phoneRegExp=/[^\d]/gi;
       var txtPhone=objPhone.value;
       txtPhone=txtPhone.replace(phoneRegExp,"");
       if (txtPhone.length ==10) 
         objPhone.value="(" + txtPhone.substr(0,3) + ") " +   txtPhone.substr(3,3) + "-" + txtPhone.substr(6,4);
      else
        alert('Phone number is invalid');
}
</script>
<form name="phoneform" action"abcaction">
<input name="phone" value="123/456/7890" onBlur="isPhone(this);">
<input type="button" name="validate" value="Validate Phone" onClick="isPhone(this.form.phone);">
</form>
</body>
</html>

Try it and see if it fits your need.

Good Luck.

grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
-----------------------------------------------
 
You brought up an interesting point grtfercho. My goal is when the input field is populated with the phone number that it would automatically remove any characters that are not numbers. Some of the phone fields have text in them (ex Phone1 or Phone 2..some folks have multiple numbers, but I only want the first one and delete the rest.) So now I am wondering would it be better do/easier to format the phone number on the server side. The other twist is that some folks will not have a number populated and will enter their number. This is where I would want to clean the data up when they hit submit. Hope I have not confused you all more. I know you all will have a solution because you always do.
 
It seems to me that a Database cleanup will more effective, formatting the value either on the server or the client will make things easier to read for your clients but you will still have a lot of junk on the tables.

ptuck said:
(ex Phone1 or Phone 2..some folks have multiple numbers, but I only want the first one and delete the rest.)

I guess you have a column Phone where more than one phone is saved
ie

Code:
[b]Phone[/b]
123/456/7890
432-123-1231, 213/312/4354
342.123.1233
(879)123-3124
and so on.

What are te conditions for your phone numbers? Are you sure all of them are 10 digits always? Is anybody using the old 7 digit format? what about 800 numbers are they using the number 1 in front?Any international numbers?
What database are you using?

Depending on the answers probably you would get better advice from a Database forum than from the this one.



grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
-----------------------------------------------
 
Could not agree with you more about the database cleanup. Unfortunately, it is a very large database (Oracle) for our company and I do not have control over it. I have linking to it from an Access DB to pull in contact information. There may be an easier way to format/validate the field when I run the query to pull the data in. If you all have any suggestions, I am open. I do think I may pose the question in the Access forum.

Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top