如何向Magento原型添加自定义验证 [英] How to add a custom validation to Magento prototype
问题描述
我想为一些自定义字段创建一个简单的url验证器.我尝试了默认的(将类validate-url
或validate-clean-url
添加到输入中)-但是它们不能按我希望的那样工作,因此我想编写一些自己的JavaScript,但与原型验证.
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 input) - but these don't work quite as I would like them to, so I want to write some of my own javascript, but integrated with the prototype validation.
有人知道我该怎么做吗?
Does anyone have any ideas how I can do this?
我在搜索中没有发现任何有用的东西,而且我也不是非常精通原型(主要是使用jQuery的人).
I didn't find anything helpful in my searches, and I am not very Prototype-savy (worked mostly with jQuery).
推荐答案
您可以使用创建自己的自定义验证功能
You can create your own custom validation function using
<script type="text/javascript">
var theForm = new VarienForm('theForm', true);
Validation.add('validate-must-be-baz','You failed to enter baz!',function(the_field_value){
if(the_field_value == 'baz')
{
return true;
}
return false;
});
</script>
请参见 http://magento-quickies.tumblr.com/post/6579512188/magento-custom-form-validation
或
if(Validation) {
Validation.addAllThese([
[
'validation-myown',
'Please insert proper word',
function(v,r){ return v.indexOf('valid')==-1?false:true }
],
[ ]
])
}
请参见 http://blog.baobaz.com/en/blog/custom-javascript-表单验证器
这篇关于如何向Magento原型添加自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!