简体   繁体   English

如何在 html 表单中的所有空白和必填字段显示“请填写此字段”?

[英]How to display “Please fill out this field” for all empty and required fields in html form?

I have a form as shown in the fiddle https://jsfiddle.net/vrn7zx5h/3/ in which I want to show the warning sign "Please fill out this field" at the same time for all unfilled required fields.我有一个如小提琴https://jsfiddle.net/vrn7zx5h/3/中所示的表格,我想在其中显示警告标志“请填写此字段” ,同时为所有未填写的必填字段。

I found the answer on SO (as shown below) but i am not sure how to integrate with the fiddle.我在SO上找到了答案(如下所示),但我不确定如何与小提琴集成。

function checkName(val){
        if(/^[^-\s][\w\s]+$/.test(val)){
            return true;
        }else{
            if(val.length != 0){
                return false;
            }
        }
    }

Problem Statement:问题陈述:

I am wondering what changes I should make in the fiddle so that the above pasted SO answer works with the fiddle.我想知道我应该在小提琴中进行哪些更改,以便上面粘贴的SO 答案适用于小提琴。

Here is a JS fiddle that will show all error at one time.这是一个 JS fiddle,它将一次显示所有错误。 It is just barebone and not fancy.它只是准系统而不是花哨的。 You'll need to make it fancy on your own.您需要自己制作它。 I also disabled the built-in validator as well with novalidate in the form tag.我还禁用了内置验证器以及表单标签中的novalidate https://jsfiddle.net/6kxc9hmq/1/ FYI: I also did not put in the functionality to hide the error message on next run, if the input now satisfies the condition. https://jsfiddle.net/6kxc9hmq/1/仅供参考:如果输入现在满足条件,我也没有在下次运行时隐藏错误消息的功能。

Basically, I attached a submit event handler to the form and if the validator returned false, I told the form to not submit.基本上,我在表单上附加了一个提交事件处理程序,如果验证器返回 false,我告诉表单不要提交。 Works only on IE9+ (I think) all the other browsers are usually fine with this method.仅适用于 IE9+(我认为)所有其他浏览器通常都可以使用这种方法。 The validator is basically just checking if the value of the input met the condition that I specified.验证器基本上只是检查输入的值是否满足我指定的条件。

document.getElementById('form').addEventListener('submit', function(e) {
if(!validate())
    e.preventDefault();
});

I think it should look like this, if I understand what you mean如果我明白你的意思,我认为它应该看起来像这样

<form action="">
    Username: <input type="text" name="usrname">
    Password: <input type="password" name="Password">
    <input type="submit">
</form>
<p><strong>Note:</strong> The required attribute of the input tag is not
supported in Internet Explorer 9 and earlier versions.</p>
<script>
    // append the listeners
    document.forms[0].addEventListener('submit', function(evt){
        if([
            checkName(this.querySelector('[name="usrname"')),
            checkName(this.querySelector('[name="Password"'))
        ].some((v)=>v)) {
            evt.preventDefault()
        }
    })

    // check is empty, then notify
    function checkName(element){
        // if you just have to check if is empty, this is enough
        if(element.value) {
            return
        }
        notify(element)
        return true
    }

    // print the message
    function notify(element) {
        if(element.nextElementSibling.classList.contains('notify')) {
            return;
        }
        element.parentNode.insertBefore(
            Object.assign(document.createElement('p'),
            {
                className: 'notify',
                innerHTML: 'Please fill out this field for all empty and required fields'
            }
        ), element.nextSibling)
    }
</script>

In your form, add empty divs after each input element.在您的表单中,在每个输入元素后添加空 div。 And you can conditionally display messages in the div in your validation.您可以在验证中有条件地在 div 中显示消息。 Eg if(name ==' '){div.innerHTML = 'please enter your name'}例如 if(name ==' '){div.innerHTML = '请输入你的名字'}

The required Attribute Add the required attribute to your form.必需的属性 将必需的属性添加到您的表单中。

The required attribute tells the browser to only submit the form if the field in question is filled out. required 属性告诉浏览器仅在填写相关字段时才提交表单。 Obviously, this means that the field can't be left empty, but it also means that, depending on other attributes or the field's type, only certain types of values will be accepted.显然,这意味着该字段不能为空,但这也意味着,根据其他属性或字段的类型,仅接受某些类型的值。

暂无
暂无

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

相关问题 为所有空白和必填字段显示“请填写此字段” - Displaying 'Please fill out this field' for all empty and required fields 表单提交必填空字段显示消息“请填写此字段”。 - form submit required empty field shows message "Please fill out this field." 如何在 sweetalert 中查看所有表单验证错误消息,而不是消息“请填写此字段” - Jquery - How to see all form validation error messages in a sweetalert instead of message “please fill out this field” - Jquery jQuery Validate - 在工具提示中显示字段错误以及“请填写必填字段”消息 - jQuery Validate - display field errors in tooltip plus “Please fill required fields” message 验证消息“请填写此字段”显示<form novalidate></form> - Validation message "Please fill out this field" displaying with <form novalidate> 清空/空白表格页面中的所有字段 - Empty / blank out all fields in a form page 如何禁用用于输入验证的弹出窗口(“请填写此字段”)? - How to disable the popup that appears for input validation (“Please fill out this field”)? 如何删除默认文本“请填写此字段” - How to remove the default text "Please fill out this field" HTML表单必填字段 - HTML Form required fields 单击angularjs中的提交表单时如何删除“请填写此字段”弹出但其他验证应该有效 - how to remove "please fill out this field" pop up when click on submit form in angularjs but other validations should work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM