below is a snippet of code that I started with... then I made some changes based on the commented suggestion from stackoverflow user. (Please see below for my progress so far)

ORIGINAL

$valid = true;

    // basic validation
    $phoneNumber = str_replace( ' ', '', $phoneNumber );
    if ( strlen( $phoneNumber ) < 10 || !is_numeric( $phoneNumber ) ) {
        $valid = false;
    }

    $areaCode  = substr($phoneNumber, 0, 3);
    $prefix    = substr($phoneNumber, 3, 3);
    $mainPhone = substr($phoneNumber, 3, 7);

    // perform the same regex matching:
    if ($valid) {
        $regex = '/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))/';
        $valid = preg_match($regex, $areaCode);
    }
    if ($valid) {
        $regex = '/^(?!\d[1]{2}|[5]{3})([2-9]\d{2})([. -]*)\d{4}$/';
        $valid = preg_match($regex, $mainPhone);
    }

    // perform the original web validation:
    if ( $valid ) {
        // validate area code
        if ( 
             $areaCode == '000' ||
             $areaCode == '111' ||
             $areaCode == '222' ||
             $areaCode == '333' ||
             $areaCode == '444' ||
             $areaCode == '555' ||
             $areaCode == '666' ||
             $areaCode == '777' ||
             $areaCode == '999' ||
             $areaCode == '123' || (is_string($areaCode) && !is_numeric($areaCode))) {
            $valid = false;
        }
    }

    if ( $valid ) {
        // validate prefix
        if ( $prefix == '123' ||
             $prefix == '000' ||
             $prefix == '111' ||
             $prefix == '555' || (is_string($prefix) && !is_numeric($prefix))) {
            $valid = false;
        }
    }

    if ( $valid ) {
        // validate main phone number

        if ( $mainPhone == '2222222' ||
             $mainPhone == '3333333' ||
             $mainPhone == '4444444' ||
             $mainPhone == '6666666' ||
             $mainPhone == '7777777' ||
             $mainPhone == '8888888' ||
             $mainPhone == '9999999' || (is_string($phoneNumber) && !is_numeric($phoneNumber))) {
            $valid = false;
        }
    }

    return $valid;

NEW JAVASCRIPT VERSION (SO FAR)

below is a snippet of code that I am converting so far... I still have some PHP stuff in there can you guys help me out to remove/replace what this snippet needs to say to make it work?

function is_numeric(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }

function phonenumberIsValid(phonenumber)
{

var valid = true;

    // basic validation
    var phonetest = phonenumber.replace(' ','');
    if ( strlen( phonetest ) < 10 || !is_numeric( phonetest ) ) {
       valid = false;
    }

    var areaCode  = phonetest.substr(0,3);
    var prefix    = phonetest.substr(3,3);
    var mainPhone = phonetest.substr(3,7);


    // perform the same regex matching that LeadMaster does:
    if(valid){
        valid = areaCode.match('/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))/');
    }

    if(valid){
        valid = mainPhone.match('/^(?!\d[1]{2}|[5]{3})([2-9]\d{2})([. -]*)\d{4}$/');
    }

    // perform the original web validation:
    if(valid){
        // validate area code
        if ( 
             areaCode == '000' ||
             areaCode == '111' ||
             areaCode == '222' ||
             areaCode == '333' ||
             areaCode == '444' ||
             areaCode == '555' ||
             areaCode == '666' ||
             areaCode == '777' ||
             areaCode == '999' ||
             areaCode == '123' || (!is_numeric(areaCode)) {
             valid = false;
        }
    }

    if(valid) {
        // validate prefix
        if ( prefix == '123' ||
             prefix == '000' ||
             prefix == '111' ||
             prefix == '555' || (!is_numeric(prefix)) {
             valid = false;
        }
    }

    if(valid) {
        // validate main phone number

        if ( mainPhone == '2222222' ||
             mainPhone == '3333333' ||
             mainPhone == '4444444' ||
             mainPhone == '6666666' ||
             mainPhone == '7777777' ||
             mainPhone == '8888888' ||
             mainPhone == '9999999' || (!is_numeric(phoneNumber)) {
             valid = false;
        }
    }

    return valid;

}
share|improve this question

1 Answer

PregMatch can be replaced with "myString".match so for instance.

if ($valid) {
    $regex = '/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))/';
    $valid = preg_match($regex, $areaCode);
}

would become

if(valid){
  valid = areaCode.match('/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))/');
}

and

str_replace("search","replace",$myString) 

becomes

myString.replace("search","replace") 

In fact most of this can be worked out yourself by typing things like "str_replace javascript" into google and looking for the previous stack overflow answer :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.