简体   繁体   English

jQuery Post和HTML表单验证

[英]Jquery post and html form validation

I $.post(); 我$ .post(); correctly some forms elements with jquery. 正确地用jQuery一些表格元素。 Now, some of these elements are "required", of course I can test server side if they are correctly filled and return back a response, but it would be nice if I could trigger the classic HTML required error, where it also evidences the missing fields. 现在,其中一些元素是“必需的”,我当然可以测试服务器端是否正确填充了它们并返回响应,但是如果能够触发经典的HTML必需的错误(也可以证明缺少该错误)会很好领域。 Of course I'll test them serverside after, but at least I don't have to do the highlighting with javascript. 当然,之后我将在服务器端对其进行测试,但是至少我不必使用javascript进行突出显示。

I don't like creating an hidden post button. 我不喜欢创建隐藏的帖子按钮。 Also because I post more than one form at once 同样是因为我一次发布多个表格

formX.serialize() + formX+1.serialize() + ...

I'd like to know if there is a way. 我想知道是否有办法。 Thanks to you. 谢谢你。

You can also use the serializeArray method in jQuery as well.: https://api.jquery.com/serializeArray/ 您也可以在jQuery中使用serializeArray方法。: https : //api.jquery.com/serializeArray/

Example: 例:

$(document).ready(function() {

var oParams = {
    'invalid_msgs' : {
      'title' : 'Kindly provide the title',
      'body' : 'Kindly provide the body',
      'type' : 'Kindly provide the type'
  },
};

$('#frm-add-content').submit(function(event) {
    event.preventDefault();
    var oFormData = $(this).serializeArray();
    // console.log(oFormData);

    for (var iIndex in oFormData) {

        if (oFormData.hasOwnProperty(iIndex) === true) {
            var sInputKeys = oFormData[iIndex].name; // (e.g title)
            var sInputValues = oFormData[iIndex].value; // (e.g Title 1)

            /* If in case there are [input] fields that
             * are not required in this case, You can set
             * condition like this
             * In this case, The input that had the [author] 
             * name attribute will be 'exempted' for validation
             */
            var aNotRequiredInputKeys = ['author'];
            if ($.inArray(sInputKeys, aNotRequiredInputKeys) < 0) {
                if ($.trim(sInputValues) === '') {
                    alert(oParams.invalid_msgs[sInputKeys]);
                    return false;
                }
            }

        }
    }

});

});

Here's a jsfiddle for reference: https://jsfiddle.net/xdp70kt3/2/ 这是一个jsfiddle供参考: https ://jsfiddle.net/xdp70kt3/2/

Hope this will guide you well. 希望这对您有帮助。

On the other note, You can also add required html attribute as well if you only want to check if the values were inputted. 另一方面,如果只想检查是否输入了值,则还可以添加required html属性。

You can use this jQuery plugin to validate your form: https://jqueryvalidation.org 您可以使用此jQuery插件来验证您的表单: https : //jqueryvalidation.org

You can do something like this: 您可以执行以下操作:

$("#formX").validate({
   submitHandler: function(form) {
      $(form).ajaxSubmit();
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM