当任何值不准确时,我们如何停止 HTML 表单提交按钮
当 HTML 表单元素(如验证码)出现任何错误时,我想停止提交按钮功能。我们的脚本中有几个函数,让我详细解释一下。
- 仅检查使用 PHP 配置的验证码安全代码(通过 - jquery-1.3.2.js && - validate.js)
- 通过 AJAX 调用发送数据(通过 - ajaxSubmit.js)
现在我想停止脚本,当任何用户在验证码文本中填写了错误的值时。我们只是简单地禁用提交按钮并收到消息(“表格未正确填写...”)
更新的验证 - 请仅使用验证码进行检查
<script type="text/javascript">
$.validator.addMethod('myEqual', function (value, element) {
if ($('#password-password').val() == $('#password-password-confirm').val()) {
return true;
} else return false;
}, 'Your password does not match.');
$(document).ready(function() {
$('#password-clear').show();
$('#password-password').hide();
$('#password-clear').focus(function() {
$('#password-clear').hide();
$('#password-password').show();
$('#password-password').focus();
});
$('#password-password').blur(function() {
if($('#password-password').val() == '') {
$('#password-clear').show();
$('#password-password').hide();
}
});
$('#password-clear-confirm').show();
$('#password-password-confirm').hide();
$('#password-clear-confirm').focus(function() {
$('#password-clear-confirm').hide();
$('#password-password-confirm').show();
$('#password-password-confirm').focus();
});
$('#password-password-confirm').blur(function() {
if($('#password-password-confirm').val() == '') {
$('#password-clear-confirm').show();
$('#password-password-confirm').hide();
}
});
var validator = $("#signupform").validate({
//ignore: ".ignore",
rules: {
username: {
required: true,
minlength: 5
},
captcha: {
required: true,
remote: "includes/process.php"
},
password: {
required: true,
minlength: 5
},
passwordconfirm: {
required: true,
minlength: 5,
myEqual: true
},
email: {
required: true,
email: true
}
},
messages: {
captcha: "Correct captcha is required. Click the captcha to generate a new one",
username: {
required: "Enter a username",
minlength: jQuery.format("Enter at least {0} characters"),
},
password: {
required: "Provide a password",
rangelength: jQuery.format("Enter at least {0} characters")
},
passwordconfirm: {
required: "Provide a password",
rangelength: jQuery.format("Enter at least {0} characters")
},
email: {
required: "Please enter a valid email address",
minlength: "Please enter a valid email address"
}
},
// the errorPlacement has to take the table layout into account
errorPlacement: function(error, element) {
if ( element.is(":radio") )
error.appendTo( element.parent().next().next() );
else if ( element.is(":checkbox") )
error.appendTo ( element.next() );
else
error.appendTo( element.parent().next() );
},
submitHandler: function() {
alert("submitted!");
},
// specifying a submitHandler prevents the default submit, good for the demo
// set this class to error-labels to indicate valid fields
success: function(label) {
// set as text for IE
label.html("").addClass("checked");
// form.submit();
}
});
});
请建议我适合我们要求的正确代码。
I would like to stop the submit button function, when we got any error in HTML form elements (like captcha). We have several functions in our script, let me explain in details.
- Check the data validation only captcha security code configured with PHP (through - jquery-1.3.2.js && - validate.js)
- Send data through AJAX call (through - ajaxSubmit.js)
now i would like to stop the script, when any of the user filled the wrong value in Captcha text. we just simply disable the submit button and get message ("Form Not Filled Properly...")
UPDATED VALIDATION - please check only with Captcha codes
<script type="text/javascript">
$.validator.addMethod('myEqual', function (value, element) {
if ($('#password-password').val() == $('#password-password-confirm').val()) {
return true;
} else return false;
}, 'Your password does not match.');
$(document).ready(function() {
$('#password-clear').show();
$('#password-password').hide();
$('#password-clear').focus(function() {
$('#password-clear').hide();
$('#password-password').show();
$('#password-password').focus();
});
$('#password-password').blur(function() {
if($('#password-password').val() == '') {
$('#password-clear').show();
$('#password-password').hide();
}
});
$('#password-clear-confirm').show();
$('#password-password-confirm').hide();
$('#password-clear-confirm').focus(function() {
$('#password-clear-confirm').hide();
$('#password-password-confirm').show();
$('#password-password-confirm').focus();
});
$('#password-password-confirm').blur(function() {
if($('#password-password-confirm').val() == '') {
$('#password-clear-confirm').show();
$('#password-password-confirm').hide();
}
});
var validator = $("#signupform").validate({
//ignore: ".ignore",
rules: {
username: {
required: true,
minlength: 5
},
captcha: {
required: true,
remote: "includes/process.php"
},
password: {
required: true,
minlength: 5
},
passwordconfirm: {
required: true,
minlength: 5,
myEqual: true
},
email: {
required: true,
email: true
}
},
messages: {
captcha: "Correct captcha is required. Click the captcha to generate a new one",
username: {
required: "Enter a username",
minlength: jQuery.format("Enter at least {0} characters"),
},
password: {
required: "Provide a password",
rangelength: jQuery.format("Enter at least {0} characters")
},
passwordconfirm: {
required: "Provide a password",
rangelength: jQuery.format("Enter at least {0} characters")
},
email: {
required: "Please enter a valid email address",
minlength: "Please enter a valid email address"
}
},
// the errorPlacement has to take the table layout into account
errorPlacement: function(error, element) {
if ( element.is(":radio") )
error.appendTo( element.parent().next().next() );
else if ( element.is(":checkbox") )
error.appendTo ( element.next() );
else
error.appendTo( element.parent().next() );
},
submitHandler: function() {
alert("submitted!");
},
// specifying a submitHandler prevents the default submit, good for the demo
// set this class to error-labels to indicate valid fields
success: function(label) {
// set as text for IE
label.html("").addClass("checked");
// form.submit();
}
});
});
Please suggest me proper code for our requirement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 jQuery:
With jQuery:
使用表单的 onSumbit 属性。为其分配用于检查表单填写是否正确的函数。如果表单填写正确则返回 true;否则返回 false。 False 将停止提交表单。您可以在返回 false 之前显示所需的消息。
Use the onSumbit attribute o fteh form. Assign it the function that you use to check for correct form completion. If the form is correctly filled out then return true; otherwise return false. False will stop the form from being submitted. You could display the required message just before returning false.