2011-08-19 76 views
1

下面是我開始與...然後我做了基於從計算器用戶評論建議一些變化的代碼片段。 (請參見下面的我的進步至今)需要藉助PHP轉換的代碼片段爲Javascript

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; 

新的JavaScript版本(SO FAR)

下面

是一個代碼片段,我至今轉換......我還是在那裏有一些PHP的東西你們可以幫我刪除/替換一下這個片段需要說的,使其工作?

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; 

} 

回答

2

例如,可以用「myString」.match替換PregMatch。

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

將成爲

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

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

成爲

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

事實上,大多數的,這可以通過鍵入像 「str_replace函數的javascript」 制定出自己進入谷歌和廁所王前一個堆棧溢出的答案:)