0

I need to do a validation on form submit, Which needs values from two different fields in the same form. I thought I will write a validator for one element and will read other element value with jQuery and validate. Is there a Magento way to do it?

I add adding validator like this -

Validation.add("string", "string", function(v){})

1 Answer 1

0

Magento 1.x does not come with jQuery validator natively but with Prototype that permits to validate form the way you describe. To check the different validators avialable you can have look at js/prototype/validation.js on line 414. You can add your own by doing something like that in foo.js (I did not test the regex but I think it is not the point) :

Validation.add('validate-foo', 'The entry must be foo', function(v) {
    return Validation.get('IsEmpty').test(v) || /^([a-zA-Z0-9._%-]*foo[a-zA-Z0-9._%-]*)$/i.test(v)
})

Then you need to add the js in your layout :

<default>
    <reference name="head">
        <action method="addJs"><script>foo.js</script></action>
    </reference>
</default>
6
  • Magento 1.x does not come with jQuery natively : Not correct. jQuery has been inlcluded natively since 1.9 (RWD) - ref: devdocs.magento.com/guides/m1x/ce19-ee114/RWD_dev-guide.html
    – ProxiBlue
    Commented Nov 11, 2015 at 9:32
  • my assertion is correct and your link confirms it : jQuery: Used for all custom JavaScript in the responsive theme. jQuery operates in noConflict() mode so it doesn't conflict with Magento's existing JavaScript library. The validation natively is held by prototype... Commented Nov 11, 2015 at 9:37
  • But it IS included, natively, and can be used by 3rd party / custom code. Yes it is not used for the validation, but that does not mean it is not included natively. Your statement should thus read: Magento <1.9 does not come with jQuery natively.... If you use magento 1.9+ you do not need to load jQuery additionally, as is the case prior to magento 1.9. If someone reads your answer, they may think jQuery is not included (at all) - it is incorrect.
    – ProxiBlue
    Commented Nov 11, 2015 at 9:42
  • 1
    I added "validator" to my answer to make it more clear. Commented Nov 11, 2015 at 9:47
  • This answer does not provide any information on the scenario in question where form validity can only be determined by considering multiple form elements' values in one validation rule. The OP did not ask for a generic "add validator how-to".
    – mam08ixo
    Commented Nov 11, 2015 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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