2013-05-17 138 views
0

我的客戶希望使用(###)### - ####格式統一結賬(訪客或註冊用戶)時輸入的所有客戶電話號碼。我發現了一段代碼,我相信會工作,但我不確定如何將其實現到Magento窗體中,以便它正確執行,並且不會在用戶提交表單時混淆進入Magento的數據。我也意識到存儲這樣的數字而不是1234567890不是最佳實踐,原因很多,但這是客戶想要的。任何幫助,將不勝感激。在Magento中格式化客戶電話號碼

文件我編輯: 模板/結算/ onepage/billing.phtml

行代碼:

<div class="field"> 
<label for="billing:telephone" class="required"><em>*</em><?php echo $this->__('Telephone') ?></label> 
<div class="input-box"> 
<input type="text" name="billing[telephone]" value="<?php echo $this->escapeHtml($this->getAddress()->getTelephone()) ?>" title="<?php echo $this->__('Telephone') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>" id="billing:telephone" /> 
</div> 
</div> 

代碼我發現,我想要實現: http://www.weberdev.com/get_example.php3?ExampleID=3605

<?php function phone_number($sPhone){ 
$sPhone = ereg_replace("[^0-9]",'',$sPhone); 
if(strlen($sPhone) != 10) return(False); 
$sArea = substr($sPhone,0,3); 
$sPrefix = substr($sPhone,3,3); 
$sNumber = substr($sPhone,6,4); 
$sPhone = "(".$sArea.")".$sPrefix."-".$sNumber; 
return($sPhone); 
} 
print phone_number(" (555) 555 - 1212 "); 
?> 

回答

0

在添加了這個html後,在輸入字段中添加validate-phoneLax類,如下所示。

<input type="text" name="billing[telephone]" value="<?php echo $this->escapeHtml($this->getAddress()->getTelephone()) ?>" title="<?php echo $this->__('Telephone') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?> validate-phoneLax" id="billing:telephone" /> 

刷新頁面,如果仍然不比你需要定義表單, 對於添加下面的代碼工作。並更新您的表單ID。

<script type="text/javascript"> 
     var myForm = new VarienForm('your_form_id_here'); 
    </script> 

這應該工作

+0

感謝您的答覆DepH。 – Matt

-2

簡單的解決辦法:只使用jquery validation.js ...設置你的類領域包括使用,你將能夠「驗證-phoneStrict」

使用下面的代碼規則在JS:

'validate-phoneStrict', 'Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890.', function(v) { 
       return Validation.get('IsEmpty').test(v) || /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/.test(v); 
0

轉到原型/ validate.js並找到

['validate-phoneStrict', 'Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890.', function(v) { 
       return Validation.get('IsEmpty').test(v) || /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/.test(v); 
      }], 

並更換

[ 'validate-phoneStrict', 
     'Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890.', 
     function(v) 
     { 
      return Validation.get('IsEmpty').test(v) 
       || /\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[]|8[6421]|6[]|5[87654321]|4[987654310]|3[]|2[70]|7|1)\d{1,14}$/.test(v); 
     } 
    ]