2011-09-13 63 views
0

我試圖驗證輸入字段是否爲特定域的電子郵件添加,並在條件未滿足時清除字段的內容。但輸入字段不會使用'onblur'事件清除。這裏是我的代碼:在magento註冊表中的字段驗證

<input type="text" name="email" onBlur="checkForm();" onSubmit="checkForm()"id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="input-text validate-email required-entry" /> 
         <?php echo "Enter a valid Sonata-software.com E-mail Id"?> 

<script type="text/javascript"> 
    function validateEmail(elementValue){ 
     var emailPattern = /^[a-zA-Z0-9._-][email protected]$/; 
     return emailPattern.test(elementValue); 
    } 
    function clear(){ 
    document.getElementById("email_address").value = ""; 
    } 
    function checkForm(){ 
     var emailId = document.getElementById('email_address').value; 
     if(! validateEmail(emailId)) { 
      alert("Enter a valid Sonata-software.com E-mail Id"); 
     clear(); 
        } 
    } 
</script> 
+0

你得到任何JS錯誤? –

+0

不,我沒有得到任何錯誤,但是當我使用alert語句來檢查我的清除函數是否被調用時,我收到一條消息:阻止此頁面使用複選框創建aditional對話框。如果我刪除警報我沒有得到該消息,但明確的功能似乎被稱爲。 – Nithin

+0

'emailId'是否包含任何內容? –

回答

0

我做到了,僅僅通過修改兩個文件:

  1. /public_html/app/design/frontend/YOURTEMPLATE/default/template/persistent/customer/form/register.phtml(添加new_class這裏:<input type="text" name="email" id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail())? >" title="<?php echo $this->__('Email Address')? >" class="input-text validate-email required-entry new_class" />

  2. /public_html/js/prototype/validation.js(添加new_class看起來像這樣:

    ['new_class', 'Only sonata-software.com accounts are allowed.', function (v) { 
          return Validation.get('IsEmpty').test(v) || /^[a-zA-Z0-9._-][email protected]$/.test(v) 
         }], 
    

請告訴我它是否適合你。