当任何值都不准确时,我们如何停止HTML表单提交按钮?

How can we stop the HTML Form Submit button, when any of the value will not be accurate

本文关键字:HTML 何停止 表单提交 按钮 我们 任何值 不准确      更新时间:2023-09-26

当我们在HTML表单元素(如captcha)中得到任何错误时,我想停止提交按钮功能。在我们的脚本中有几个函数,让我详细解释一下。

  1. 检查数据验证仅使用PHP配置的验证码安全码(通过- jquery-1.3.2.js &&- validate.js)
  2. 通过AJAX调用发送数据(through - 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 &nbsp; as text for IE
        label.html("").addClass("checked");
                      //  form.submit();
    }
});
 });

请根据我们的要求建议合适的代码。

使用jQuery:

$("#formid").submit(function (){
  if(!your condition){
    return false;
  }
});

使用表单的onSumbit属性。为它分配用于检查表单是否正确完成的函数。如果表格填写正确,则返回true;否则返回false。False将停止提交表单。您可以在返回false之前显示所需的消息。