Form Validation
Subject:   Phone validation in error
Date:   2003-01-30 14:20:38
From:   anonymous2
Very good script overall and lucid explanation. However, the phone script accepts alpha characters (up to nine) for the phone number as long as the first character is numeric.

Here is the validation chunk for the phone number:
// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
error = "You didn't enter a phone number.\n";

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
if (isNaN(parseInt(stripped))) {
error = "The phone number contains illegal characters.";

if (!(stripped.length == 10)) {
error = "The phone number is the wrong length. Make sure you included an area code.\n";
return error;
Any ideas?

Mike Sivertsen
Web Architect

Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • Phone validation in error
    2003-08-01 15:11:16  anonymous2 [View]

    >> the phone script accepts alpha characters
    >>Any ideas?

    Perhaps try replacing:
    if (isNaN(parseInt(stripped)))

    with something along the lines of:
    var filter = /^([0-9}{9})$/
    if !filter.test(stripped)
    { error = "....." }

    this should (theoretically anyways;) ) return true only if the (stripped) string contains 9 numbers only.

    I am still learning JS, so any response (either confirmation or refutation) is welcomed.

    Scott TFH