•
•
•
•
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 456,571 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,617 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: 3520 | Replies: 2 | Solved
![]() |
| |
^
|
|
|
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).
At the moment, if you enter an illegal non-numeric charater the error
Any help would be appreciated
|
|
|
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
I managed to solve my problem by changing my script to the following:
javascript Syntax (Toggle Plain Text)
function checkTel (strng) { var error = ""; var stripped = strng.replace(/[\(\)\.\-\ ]/g, ""); var illegalChars = strng.replace(/[\(\)\.\-\ \0-9]/g, ""); //strips out the acceptable characters you'd find in a tel number, leaving any illegal characters. if (strng == "") { error = "Please enter your cell number.\n"; } 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. error = "The tel number contains illegal characters.\n"; } else if (stripped.length !=10) { //if the tel number is not 10 digits long then show error msg. error = "The tel number is the wrong length.\n"; } return error; }
Last edited by Venom Rush : Oct 29th, 2007 at 11:14 am.
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
EDIT: Solved my problem. Changed the above line to:
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
javascript Syntax (Toggle Plain Text)
var illegalChars = strng.replace(/[\(\)\.\-\ \0-9\+]/g, "");
EDIT: Solved my problem. Changed the above line to:
javascript Syntax (Toggle Plain Text)
var illegalChars = strng.replace(/[0-9]/g, "");
Last edited by Venom Rush : Apr 1st, 2008 at 6:37 am.
This user has a spatula. We don't know why, but we are afraid.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Setting the focus to a specific text box and other woes (C#)
- "The message you have entered is too short." (DaniWeb Community Feedback)
- form validation (PHP)
- String Splitter (Java)
- modify script (Perl)
- Strings in array [I NEED HELP] C lang. (C)
- Program Issues (C++)
- error checking of user input (C++)
- chars and floats (C)
- How to write FNVAL functions (Java)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: falid flash on xhtml trans... plz help me
- Next Thread: pass optional parameters by name


Hybrid Mode