How to add a custom validation to Magento prototype

后端 未结 2 646
自闭症患者
自闭症患者 2021-02-08 22:57

I want to make a simple url validator for some custom fields. I tried the default ones (adding the class validate-url or validate-clean-url to the inp

2条回答
  •  执笔经年
    2021-02-08 23:27

    In /js/prototype/validation.js (or the files for this kind of thing you have). You have a section with an array of :

    classname :message on fail : function(v){your check return true/false;} to check if v is valid or not

    This section is around line 420.

    You can add your validation to this array or modify validate-url here is what it looks like :

     ['validate-url', 'Please enter a valid URL. Protocol is required (http://, https:// or ftp://)', function (v) {
                v = (v || '').replace(/^\s+/, '').replace(/\s+$/, '');
                return Validation.get('IsEmpty').test(v) || /^(http|https|ftp):\/\/(([A-Z0-9]([A-Z0-9_-]*[A-Z0-9]|))(\.[A-Z0-9]([A-Z0-9_-]*[A-Z0-9]|))*)(:(\d+))?(\/[A-Z0-9~](([A-Z0-9_~-]|\.)*[A-Z0-9~]|))*\/?(.*)?$/i.test(v)
            }],
    

    Edit : R.S answered maybe better by showing how to do without changing the js file. More convenient ;)

提交回复
热议问题