简体   繁体   English

验证表单中的文本区域输入

[英]validation of textarea input in form

I leave here an example of the problem I have in validating my form with textarea.我在这里留下一个我在使用 textarea 验证表单时遇到的问题的示例。 When I click to send the form without filling in the textarea, the warning that the field is mandatory, this message appears above the label as shown in the example.当我点击发送表单而不填写文本区域时,该字段为必填的警告,此消息出现在标签上方,如示例所示。 I put in the example the HTML, js, and CSS that I apply in the form.我在示例中放入了我在表单中应用的 HTML、js 和 CSS。

 $(document).on('click', '.insdenuncia', function () { if ($(".mainn__form").valid()) insertIntoDB($("form.mainn__form")); }); $.validator.setDefaults({ ignore: ":hidden:not(.chosen-select)" }); $(".mainn__form").validate({ errorClass: "my-error-class", validClass: "my-valid-class", messages: { Denuncia: { required: "Campo obrigatório!", minlength: 12 } } }); function insertIntoDB($o) { $o.serializeArray(); var form_data = new FormData(document.getElementById("main__form")); $.ajax({ url: 'insdenuncia.php', type: 'POST', cache: false, data: form_data, contentType: false, processData: false, success: function (result) { } }); }
 .main__form .form-group label { font-family: "Quicksand", sans-serif; font-size: 1.6rem; color: #575757; padding: 0 2.5rem; position: absolute; top: 50%; -webkit-transform: translateY(-50%); transform: translateY(-50%); z-index: 0; -webkit-transition: .3s all; transition: .3s all; } .main__form .form-group input, .main__form .form-group textarea { font-size: 1.6rem; border-color: #f7f7f7; padding: 0 2.5rem; position: relative; z-index: 1; background: transparent; } .main__form .form-group input:not(:placeholder-shown)+label, .main__form .form-group textarea:not(:placeholder-shown)+label { top: 2%; background: #fff; z-index: 2; font-weight: 600; } .main__form .form-group input:focus, .main__form .form-group textarea:focus { outline: none; -webkit-box-shadow: none; box-shadow: none; } .main__form .form-group input:focus+label, .main__form .form-group textarea:focus+label { background: #fff; z-index: 2; top: 2%; font-weight: 600; } .main__form .form-group.form-message label { font-family: "Quicksand", sans-serif; font-size: 1.6rem; color: #575757; padding: 2.5rem; position: absolute; top: -12%; -webkit-transform: unset; transform: unset; z-index: 0; -webkit-transition: .3s all; transition: .3s all; } .main__form .form-group.form-message textarea { padding: 2.5rem; } .main__form .form-group.form-message textarea:not(:placeholder-shown)+label { top: -8% !important; background: #fff; z-index: 2; font-weight: 600; padding: 0 2.5rem !important; } .main__form .form-group.form-message textarea:focus { outline: none; -webkit-box-shadow: none; box-shadow: none; } .main__form .form-group.form-message textarea:focus+label { background: #fff; z-index: 2; top: -8% !important; font-weight: 600; padding: 0 2.5rem !important; } .main__form .form-group.form-message label { font-family: "Quicksand", sans-serif; font-size: 1.6rem; color: #575757; padding: 2.5rem; position: absolute; top: -12%; -webkit-transform: unset; transform: unset; z-index: 0; -webkit-transition: .3s all; transition: .3s all; } .main__form .form-group.form-message textarea { padding: 2.5rem; } .main__form .form-group.form-message textarea:not(:placeholder-shown)+label { top: -8% !important; background: #fff; z-index: 2; font-weight: 600; padding: 0 2.5rem !important; } .main__form .form-group.form-message textarea:focus+label { background: #fff; z-index: 2; top: -8% !important; font-weight: 600; padding: 0 2.5rem !important; }
 <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <link href="https://cdn.jsdelivr.net/npm/pixeden-stroke-7-icon@1.2.3/pe-icon-7-stroke/dist/pe-icon-7-stroke.min.css" rel="stylesheet"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script> <form class="main__form mainn__form" id="main__form" method="POST" enctype="multipart/form-data"> <div class="form-group form-message"> <textarea class="form-control" id="Denuncia" name="Denuncia" rows="6" placeholder="Message" required></textarea> <label for="Denuncia">Digite a sua denúncia <span style="color: red;">*</span></label> </div> <div class="text-right"> <button type="button" class=" btn btn-get insdenuncia"><span> Enviar</span></button> </div> </form>

The goal was for the mandatory message to appear just below the textarea and in red, if possible.目标是让强制性消息出现在文本区域的正下方,并尽可能以红色显示。 Can you help?你能帮我吗?

You are using the JQuery Validation Plugin where you can find the documentation here:您正在使用 JQuery 验证插件,您可以在此处找到文档:

https://jqueryvalidation.org/validate/ https://jqueryvalidation.org/validate/

https://jqueryvalidation.org/documentation/ https://jqueryvalidation.org/documentation/

https://jqueryvalidation.org/ https://jqueryvalidation.org/

The plugin is supposed to be used calling the validate() function over the element you wish to have the validation features (your form).该插件应该用于在您希望具有验证功能的元素(您的表单)上调用validate()函数。

Once initialized like that, when the submit event occurs for the given form, the validation logic will be applied.一旦像这样初始化,当给定表单发生提交事件时,将应用验证逻辑。

I strongly simplified your code to have only the form and its single textarea field.我强烈简化了您的代码,使其只有表单及其单个 textarea 字段。 The css rules also were stripped off because it wasn't the focus of the issue. css 规则也被剥离,因为它不是问题的焦点。

So that when you submit the form, if there are no validation errors, your submit logic will be called, otherwise if the validation hasn't passed, the general error logic gets called and in addition the error of each invalid field will be printed in a specific label created ad hoc just after the target field and having the css class error that you can style arbitrarily (I used text-color: red !important )这样当你提交表单时,如果没有验证错误,你的提交逻辑将被调用,否则如果验证没有通过,一般的错误逻辑被调用,此外每个无效字段的错误将被打印在一个特定的标签在目标字段之后临时创建,并且具有可以任意设置样式的 css 类error (我使用text-color: red !important

 //this must be called once at page load.. //and it will handle the form validation when the form will be submitted //docs here: https://jqueryvalidation.org/validate/ $("#mainform").validate({ submitHandler: function(form) { /*your logic when the form submit passed validation*/ insertIntoDB(form) }, invalidHandler: function(event, validator) { /*your logic when the form submit didn't pass validation*/ console.log('errors found when trying to submit the form!'); }, /* //this way you could control where the error will be placed errorPlacement: function(error, element) { },*/ }); function insertIntoDB($o) { /*your code to perform the server side action*/ console.log('ajax request performed!') }
 .page{ margin: 3rem; font-family: "Quicksand", sans-serif; font-size: 1.6rem; border: dashed 3px gray; padding: 1rem; } .formgroup label { display:block; color: #575757; } .formgroup input, .formgroup textarea { border-color: #f7f7f7; } .error{ color:red !important; }
 <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <link href="https://cdn.jsdelivr.net/npm/pixeden-stroke-7-icon@1.2.3/pe-icon-7-stroke/dist/pe-icon-7-stroke.min.css" rel="stylesheet"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script> <div class="page"> <form id="mainform" class="formgroup" enctype="multipart/form-data"> <div class="fieldgroup"> <textarea class="form-control" id="Denuncia" name="Denuncia" rows="6" placeholder="Message" required></textarea> <label for="Denuncia">Digite a sua denúncia <span style="color: red;">*</span></label> </div> <div class="text-right"> <!-- this must be a button type=submit to trigger the form submit --> <button type="submit" class=" btn btn-get insdenuncia"><span> Enviar</span></button> </div> </form> </div>

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

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