简体   繁体   中英

Field validation in magento registration form

i am trying to validate a input field for email add of a specific domain and clearing the contents of the field when the condition is not met. But the input field is not cleared using the 'onblur' event. Here is my code :

<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._-]+@sonata-software.com$/;  
       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>

I did it simply by modifying two files:

  1. /public_html/app/design/frontend/YOURTEMPLATE/default/template/persistent/customer/form/register.phtml (added new_class here: <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 (added new_class which looks like this:

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

Please tell me if it works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM