Field validation in magento registration form

Field validation in magento registration form

September 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Field validation in magento registration form

Field Validation in Magento Registration Form: A Comprehensive Guide

Validating form fields is an essential part of web development that ensures data accuracy and security. In this post, we will dive into the specifics of validating an email field for a specific domain in a Magento registration form.

The Challenge: Clearing the Email Field on Blur

You’re trying to validate an input field for an email address belonging to the ‘sonata-software.com’ domain using the ‘onblur’ event. However, even though your JavaScript function checkForm() is triggered, the input field isn’t being cleared when the validation fails.

Let’s break down the issue and find a solution.

Understanding the Problem

The key problem lies in the way you’re calling the JavaScript function checkForm(). You’re using both onBlur and onSubmit events for the same function. However, only one of these should be used. Since onSubmit is typically used with form submission, it might not be getting triggered when you expect.

Additionally, your current approach uses inline JavaScript, which can make the code harder to maintain and debug.

The Solution: Refactoring the Validation Logic

To resolve this issue, we need to refactor the validation logic. Here’s a step-by-step solution:

  1. Separate Form and Field Validations: It’s best to validate the form as a whole (using onSubmit) and also validate specific fields separately (using onBlur).

  2. Use Data Attributes for JavaScript: Instead of inline JavaScript, use data attributes to associate your JavaScript with the HTML elements.

  3. Refactor the JavaScript Function: Update your checkForm() function to handle both validation and clearing of the field.

  4. Use document.querySelector(): This method is more efficient than using getElementById().

Here’s how you can implement this:

<input type="text" name="email"
  data-val-rule="required|regex:sonataSoftwareEmail"
  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">
    var emailPattern = /^[a-zA-Z0-9._-]+@sonata-software\.com$/;

    function validateEmail(elementValue) {
        return emailPattern.test(elementValue);
    }

    document.querySelector('#email_address').addEventListener('blur', function() {
        var emailId = this.value;
        if (!validateEmail(emailId)) {
            alert("Enter a valid Sonata-software.com E-mail Id");
            this.value = "";
        }
    });
</script>

Key Improvements

1. Separation of Concerns

By using data attributes for validation rules and JavaScript for field-specific validation, we’ve separated the concerns of form and field validation.

2. Improved Performance with querySelector

Using document.querySelector() instead of getElementById() allows for more efficient handling of multiple elements with the same ID in the future.

Conclusion

Field validation is a critical aspect of web development that ensures data integrity and security. In this guide, we’ve demonstrated how to effectively validate an email field for a specific domain using Magento’s form validation capabilities and custom JavaScript logic.

Remember, when dealing with form validations:

  • Use the appropriate events (onSubmit for form submission and onBlur for field-specific validation).
  • Avoid inline JavaScript in favor of data attributes for better maintainability.
  • Separate form and field validations to ensure all rules are applied correctly.

I encourage you to explore this technique further on LaravelCompany, where we regularly share more advanced Magento development strategies.

Security Email Errors JavaScript

Our network