How to add a custom validation to Magento prototype

How to add a custom validation to Magento prototype

July 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add a custom validation to Magento prototype

Extending Magento Prototype Validation for Custom URL Fields

Hello developers! In this article, we’ll dive into extending the native Magento prototype validation to create a custom URL validator that fits your specific needs. I know you’re eager to get started, so let’s jump right in.

The Challenge: Default Validators Aren’t Cutting It

The built-in validate-url and validate-clean-url classes are great starting points, but sometimes they don’t quite hit the mark for our custom fields. That’s where writing your own JavaScript validator comes in handy—integrated with prototype validation, of course.

The Goal: A Prototype-Savy Custom URL Validator

We want to create a new validation class that builds upon Magento’s existing validation infrastructure. This way, we can leverage the power of Prototype while still having fine-grained control over our custom URL field validations.

Let’s Get Our Hands Dirty: Writing the Custom Validator

First things first—let’s define our new validator in JavaScript:

Validator.add('url-our-own-style', function(value, element) {
    var urlRegex = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.([a-z]{2,5})(:[0-9]{1,6})?(\/[\w\-_ %?.]*)*$/i;
    return urlRegex.test(value);
}, 'Please enter a valid URL.');

Here’s what we’ve done:

  1. We define a new validator called url-our-own-style.
  2. The first argument is the value to be validated.
  3. The second argument is the HTML element being validated.
  4. We create a regular expression (urlRegex) that matches our specific URL pattern—feel free to tweak this as needed.
  5. We use the test method to check if the input value matches our regular expression.
  6. If it doesn’t match, we return an error message.

Integrating Our Custom Validator into Magento

Okay, great! But how do we actually apply this new validator to our custom fields in Magento? Here’s where we’ll leverage the validate attribute:

<input type="text" id="my-url-field" name="my_url_field" class="input-text validate-url-our-own-style">

Note that we’ve added a new class validate-url-our-own-style. This tells Magento to use our custom validator.

The Final Touches: Adding the New Class

To make this work, we need to add our custom validator class to the validation script. You can do this in your theme’s layout XML file:

<reference name="head">
    <action method="addJs"><file>your-validator.js</file></action>
</reference>

Wrapping Up: A Powerful Custom Validation Solution

There you have it—a powerful custom URL validator integrated with Magento prototype validation. By following this approach, you can create validators that perfectly match your project’s needs while still leveraging the strengths of Prototype.

Remember to always keep your code clean and organized for better maintainability in the long run.

Happy validating!

Further Reading

For more advanced techniques on customizing Magento’s JavaScript and validation infrastructure, check out our comprehensive guide at Laravel Company.

JavaScript

Our network