格式客户电话号码在Magento


Format customer phone numbers in Magento

我的客户希望在退房时输入的所有客户电话号码(客人或注册用户)使用(###)###-####格式统一。我发现了一个代码片段,我相信会工作,但我不确定如何实现它到Magento表单,使其正确执行,而不是搞砸了数据进入Magento当用户提交表单。我还意识到,由于许多原因,像这样存储数字而不是1234567890并不是最佳实践,但这正是客户想要的。如有任何帮助,不胜感激。

正在编辑的文件:模板/付款/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 "); 
?>

在输入字段中添加validate-phoneLax类后,您的html将如下所示:

<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>

应该可以

进入prototype/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[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)'d{1,14}$/.test(v);
      }
    ]

简单修复:只需使用jquery validate .js…将字段的类设置为包含'validate-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);