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

发布于 2024-12-11 08:26:06 字数 3558 浏览 0 评论 0原文

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

  1. 仅检查使用 PHP 配置的验证码安全代码(通过 - jquery-1.3.2.js && - validate.js)
  2. 通过 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 &nbsp; 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.

  1. Check the data validation only captcha security code configured with PHP (through - jquery-1.3.2.js && - validate.js)
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法唧唧 2024-12-18 08:26:06

使用 jQuery:

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

With jQuery:

$("#formid").submit(function (){
  if(!your condition){
    return false;
  }
});
再见回来 2024-12-18 08:26:06

使用表单的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文