0

Before registering I would like to validate the email field entered by the user to know if it exists in the database.

This is my code: <input type="text" name="emailreg" value="" id="emailPreReg" class="input-text required-entry validate-email" title="" />

<button class="button2" id="btm-procedi"><span><span><?php echo $this->__('Proceed'); ?></span></span></button>

I would like the email field when the user clicks on button to have a check if email already exists in the database. In this case I would like to put an error message in the display block under the email input field.

My question is: Can i do this validation with Ajax/jQuery without custom modules?

Thanks for any help

  • what do you want here please clarify more – Rakesh Donga Apr 3 at 9:16
  • I clarified my question! – Gabriel91 Apr 3 at 9:24
  • i will hint you? you can get it enter email id? – Rakesh Donga Apr 3 at 10:21
1

You will need some js code, and some php controller/action (because you will have to send your ajax request to these action).

I suppose that you have a template for your form. So I will begin with a form :

<form id="your-form-id" action="<?php echo $this->getUrl('your-router/your-controller/your-action')" method="post">
<input type="text" name="emailreg" value="" id="emailPreReg" class="input-text required-entry validate-email" title="" />
<button class="button2" id="btm-procedi"><span><span><?php echo $this->__('Proceed'); ?></span></span></button>
</form>

and some javascript in your template too :

<script type="text/javascript">
//<![CDATA[
var yourForm = new VarienForm('your-form-id', true);
var yourUrlForAjax = '<?php echo $this->getUrl('your-router/your-controller/your-action-for-ajax') ?>';
//]]>
</script>```

For this page, you will have to add a js file (in order to listen the event "click on my button" and to send the ajax request).

In this js file, you will have something like :

$j('#btm-procedi').click(function(){
        if(yourForm.validator.validate()){
            $j.ajax({
                url: yourUrlForAjax,
                type: 'post',
                data:$j('#your-form-id').serialize(),
                dataType : 'json',
                success: function(json) {
                    if(json.type == 'success') {
                        // manage success : ? send the form to another action ?
                    }
                    else {
                        // manage error : add a message,etc ... 
                    }
                }
            })
        }
    });

Finally, you have to create an action in your controller to check if the email already exists and return a json response. Something like :

public function your-action-for-ajaxAction()
{
        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();
            $email = (isset($data['emailreg']))?$data['emailreg']:'';
            $response = array();
            // Check if email exists
            // If email do not exist :
            $response['type'] = 'success';
            // else
            $response['type'] = 'error';
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
            }
        }
}

Hope it helps.

  • Hi man, thanks for reply. So with yourUrlForAjax can i connect to default controller of Magento for validate email without create others custom controller? In this case which? – Gabriel91 Apr 3 at 11:20
  • No, sorry. I understood without custom modules as without third part module. You will have to create a custom controller. – Julien Loizelet Apr 3 at 12:21
  • Ok man, thanks for this help! I tried with your solution and works! Great! ;) – Gabriel91 Apr 3 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.