User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 397,983 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,729 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 3043 | Replies: 2 | Solved
Reply
Join Date: Oct 2007
Posts: 174
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

Checking for illegal non-numeric characters

  #1  
Oct 29th, 2007
^
|
|
|
javascript noob

Hi there

I have the following script that strips out acceptable non-numeric characters that you would find in a telephone number and then checks to see if it is equal to ten digits. The problem I have is that I can't figure out how I would check for illegal non-numeric characters (characters you wouldn't find in a telephone number).

if (isNaN(strng)) {
		var stripped = strng.replace(/[\(\)\.\-\ ]/g, "");
		if (stripped.length !10) {
		error = "The tel number is the wrong length.\n";	
		}
	}

At the moment, if you enter an illegal non-numeric charater the error "The tel number is the wrong length.\n"; pops up which is understandable because the length of stripped would be more than 10 characters but this would clearly confuse the user.

Any help would be appreciated
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Posts: 174
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

Re: Checking for illegal non-numeric characters

  #2  
Oct 29th, 2007
I managed to solve my problem by changing my script to the following:

  1. function checkTel (strng) {
  2. var error = "";
  3. var stripped = strng.replace(/[\(\)\.\-\ ]/g, "");
  4. var illegalChars = strng.replace(/[\(\)\.\-\ \0-9]/g, ""); //strips out the acceptable characters you'd find in a tel number, leaving any illegal characters.
  5. if (strng == "") {
  6. error = "Please enter your cell number.\n";
  7. }
  8. else if (illegalChars.length != 0) { //if the length of the variable illegalChars isn't 0 then it contains illegal characters and shows the error msg.
  9. error = "The tel number contains illegal characters.\n";
  10. }
  11. else if (stripped.length !=10) { //if the tel number is not 10 digits long then show error msg.
  12. error = "The tel number is the wrong length.\n";
  13. }
  14. return error;
  15. }
Last edited by Venom Rush : Oct 29th, 2007 at 10:14 am.
Reply With Quote  
Join Date: Oct 2007
Posts: 174
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

Re: Checking for illegal non-numeric characters

  #3  
Apr 1st, 2008
I initially thought I had solved my problem but I have proven myself otherwise.

The code below doesn't seem to eliminate non-numeric characters. I can still enter !#$%&* into the input field. The only characters I want to be entered are ().-+ along with spaces and numbers
  1. var illegalChars = strng.replace(/[\(\)\.\-\ \0-9\+]/g, "");


EDIT: Solved my problem. Changed the above line to:
  1. var illegalChars = strng.replace(/[0-9]/g, "");
Last edited by Venom Rush : Apr 1st, 2008 at 5:37 am.
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 1:05 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC