如何使用jquery验证和signature_pad输入签名,如何添加检查? [英] How can I add a check if a signature is entered using jquery validation and signature_pad?
问题描述
我正在使用 jquery.validate.js 来验证表单中的多个字段以及 smizek的signature_pad 。我想验证是否在同时验证所有其他输入字段的同时在html画布(签名板上)上绘制了一些东西。
I'm using jquery.validate.js to validate multiple fields in my form along with smizek's signature_pad. I want to validate if something is drawn on the html canvas (signature pad) at the same time all the other input fields are validated.
不幸的是,在jQuery验证Submithandler来检查画布是否具有签名是不够的,因为这仅在满足所有其他必需字段时才触发。我可以将方法添加到jquery验证程序中,但是我很确定这些方法仅可用于验证输入的内容,而不是画布。
Unfortunately, adding a function to the jquery validation submithandler to check if the canvas has a signature is not sufficient because this only triggers once all other required fields are satisfied. I can add methods to jquery validator, but I'm pretty sure those only work on validating the content of inputs—not a canvas.
我可以检查签名是否存在与 signaturePad.isEmpty()。理想情况下,将有一种添加与任何验证同时发生的函数的方法:
I can check if the signature exists with "signaturePad.isEmpty()". Ideally, there would be a way to add a function like this that happens concurrently with any validation:
var errorExists = false;
if( signaturePad.isEmpty()){
if (errorExists == false){
$( "#signature-pad" ).append( "<div id="sig-error">A signature is required</div>" );
errorExists = true;
}
}else if( !signaturePad.isEmpty()) {
if (errorExists == true){
$('#sig-error').remove();
}
}
...并且仅继续提交
...and only continue on to actually submit the form if both the jquery validation and the signature are satisfied.
是否有办法在其他验证发生的同时(而不是之后)触发函数触发器?除非同时满足我的两个条件,否则如何停止提交表单-不只是jquery验证?
Is there a way to have a function trigger at the same time other validation happens—not after? How can I stop the form from submitting unless both my conditions are met—not just the jquery validation?
推荐答案
在jquery验证中submithandler,如果未填写签名,则返回false
:
In the jquery validation submithandler, return false
if the signature is not filled:
if( signaturePad.isEmpty()){
console.log('it is empty');
return false;
}
这可以防止在没有签名的情况下提交表单。
This prevents the form from being submit if there is no signature.
要像其他jquery验证错误消息一样显示缺少签名的错误消息,请将函数绑定到提交输入的单击上:
To show an error message for a missing signature like the rest of the jquery validation error messages, bind a function to the submit input's click:
$('#submit_form').click(function(){
if( signaturePad.isEmpty()){
if (errorExists === false){
$( "#signature-pad" ).append( "<div class='error' id='sig-error'>A signature is required</div>" );
errorExists = true;
}
}else if( !signaturePad.isEmpty()) {
if (errorExists === true){
$('#sig-error').remove();
errorExists = false;
}
}
});
在某处声明变量:
var errorExists = false;
这篇关于如何使用jquery验证和signature_pad输入签名,如何添加检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!