[英]How to validate form in Magento Javascript as you type
Magento has awesome Javascript validation library, which can be initialized var myForm= new VarienForm('[your form id]', true);
Magento有很棒的Javascript验证库,可以初始化
var myForm= new VarienForm('[your form id]', true);
. 。 However this validation function is triggered when one click on submit button.
但是,单击“提交”按钮时会触发此验证功能。
Is not there way to validate particular field as you type. 在您键入时无法验证特定字段。 For example if I type postal code 2 digit and go to second field, it should instantly validate postal code and show error.
例如,如果我输入邮政编码2位数并转到第二个字段,它应该立即验证邮政编码并显示错误。 As postal code require at least 5 digits.
邮政编码需要至少5位数。
thanks 谢谢
Yes, Magento provide awesome validation library. 是的,Magento提供了很棒的验证库。 You can call validation for each field with `validate' method.
您可以使用“validate”方法为每个字段调用验证。
For example to validate zip code, you can observe blur event and call validate
method. 例如,要验证邮政编码,您可以观察模糊事件并调用
validate
方法。
$('billing:postcode').observe('change', function(e){
Validation.validate($('billing:postcode'))
})
Notice Validation.validate($('billing:postcode'))
, this will call validation for postcode field. 注意
Validation.validate($('billing:postcode'))
,这将调用postcode字段的验证。
First, create for your form. 首先,为您的表单创建。
<form action="<?php echo $this->getUrl('/some/route/thing');?>" id="theForm">
<input type="text" name="foo" id="foo" />
</form>
Next, run this bit of javascript to turn your plain old form into VarienForm 接下来,运行这一点javascript将您的普通旧表单转换为VarienForm
<script type="text/javascript">
//<![CDATA[
var theForm = new VarienForm('theForm', true);
//]]>
</script>
Then, write your validation as a javascript function using the Validation.add method. 然后,使用Validation.add方法将验证写为javascript函数。 (Validation is a global used to store all form validation rules)
(验证是一个全局用于存储所有表单验证规则)
<script type="text/javascript">
//<![CDATA[
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>
You can write a custom validation class: 您可以编写自定义验证类:
Validation.add('validate-float','Error message',function(v){
return Validation.get('IsEmpty').test(v) || (!/\./.test(v));
});
see - https://magento.stackexchange.com/a/15165/4832 请参阅 - https://magento.stackexchange.com/a/15165/4832
Not adding anything new here, but if you want to cut-and-paste a quick way to create multiple validations for your form, just add to the fields
array: 这里没有添加任何新内容,但如果您想快速剪切并粘贴为表单创建多个验证,只需添加到
fields
数组:
var fields = ['firstname', 'lastname', 'telephone', 'street1', 'region_id', 'country_id', 'city', 'postcode'];
fields.map( function (fld) {
$('billing:' + fld).observe('change', function(e){
Validation.validate($('billing:' + fld))
});
});
Not 100% on how you'd implement it, but you can use Prototypes Event listener. 不是100%关于如何实现它,但你可以使用Prototypes事件监听器。 I've tried hooking in to Magento's form validation once before in order to stop multiple form submissions, the code was similar to what's below but I've changed it a bit to fit with your requirements:
为了阻止多个表单提交,我曾尝试过一次挂接到Magento的表单验证,代码类似于下面的内容,但我已经改变了一些以符合您的要求:
new Event.observe('contactForm', 'keyup', function(e){
e.stop();
if(contactForm.validator && !contactForm.validator.validate()) {
//do something here because it failed validation
return;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.