如何在文本字段中显示基本验证消息

约翰·多伊

我想在需要它的文本字段下方显示验证消息以及我找到的图像中的示例

例子

在此处输入图片说明

我的表单上有以下文本字段,它们各自的验证在Javascript 中完成

//Function to validate ticket form
function validate_form() {
    valid = true;

    if (document.ticketForm.matricula.value == "") {
        alert("Verify the data again, enter the license plate");
        valid = false;
    }

    if (document.ticketForm.nombre.value == "") {
        alert("Verify the data again, enter the name of the applicant");
        valid = false;
    }

    return valid;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<form name="ticketForm" method="post" onchange="validate_form();">
                <div id="informacionTicket" class="user">
                    <div class="card shadow mb-4">
                        <div class="card-body">
                            <div class="mb-4">
                                <div class="form-group">
                                    <label for="ticketIdAppliInput">License:</label>
                                    <input maxlength="9" required id="ticketIdAppliInput" type="text" name="matricula" onkeypress="if (isNaN(String.fromCharCode(event.keyCode))) return false;" class="form-control form-control-user" />
                                </div>
                                <div class="form-group">
                                    <label for="ticketNameAppliInput">Full name:</label>
                                    <input maxlength="100" id="ticketNameAppliInput" type="text" name="nombre" class="form-control form-control-user" />
                                </div>
                                <div class="form-group">
                                    <label for="ticketEmailAppliInput">Email:</label>
                                    <input maxlength="100" id="ticketEmailAppliInput" type="email" name="email" class="form-control form-control-user" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <button type="button" id="submit" class="btn btn-primary btn-user btn-block">Send</button>
</form>

我不想要的是alert在表格顶部显示那些烦人的东西

在此处输入图片说明

我希望消息像示例图像一样显示

更新:

试试这个可能的解决方案,但当输入一个值时,消息不再被删除

在此处输入图片说明

更新:

我尝试了@JarlikStepsto 的可能解决方案,但它仍然无法正常工作

在此处输入图片说明

function validate(field) {
    const validateables = document.getElementsByClassName('validateable');
    const input = field;
    if (!input.value == "") {
        input.classList.add('invalid');      
    } else {
        input.classList.remove('invalid');
    }

    if (!input.value == "") {
        input.classList.add('invalid');      
    } else {
        input.classList.remove('invalid');
    }
}
input {
    display: block;
}

.validation-message {
    display: none;
}

input.validateable.invalid + .validation-message {
    display: block;
    color: red;
}
<div class="form-group">
                                    <label class="required-field" name="matricula" for="ticketIdAppliInput">Matrícula:</label>
                                    <input onchange="validate(this)" maxlength="9" required="required" id="ticketIdAppliInput" type="text" name="matricula" onkeypress="if (isNaN(String.fromCharCode(event.keyCode))) return false;" class="form-control form-control-user validateable"/>
                                    <div class="validation-message">
                                        Verifique la información nuevamente, ingrese la matricula</div>
                                </div>
                                <div class="form-group">
                                    <label class="required-field" name="nombre" for="ticketNameAppliInput">Nombre completo:</label>
                                    <input onchange="validate(this)" maxlength="100" id="ticketNameAppliInput" type="text" name="nombre" class="form-control form-control-user validateable" />
                                    <div class="validation-message">
                                        Verifique la información nuevamente, ingrese el nombre
                                    </div>
                                </div>

更新 2:

I will explain it better, I have two fields that matter to me that are compulsory "Matricula" and "Nombre Completo", when I am filling out the third field I do not get the validation message, this is the code I have, will I be doing something wrong?

function validate(field) {
    const input = field;
    if (!input.value || input.value.length === 0) {
        input.classList.add('invalid');      
    } else {
        input.classList.remove('invalid');
    }
}
input {
    display: block;
}

.validation-message {
    display: none;
}

input.validateable.invalid + .validation-message {
    display: block;
    color: red;
}
<div class="form-group">
                                    <label class="required-field" name="matricula" for="ticketIdAppliInput">Matrícula:</label>
                                    <input onchange="validate(this)" maxlength="9" id="ticketIdAppliInput" type="text" name="matricula" onkeypress="if (isNaN(String.fromCharCode(event.keyCode))) return false;" class="form-control form-control-user validateable"/>
                                    <div class="validation-message">
                                        Verifique la información nuevamente, ingrese la matricula</div>
                                </div>
                                <div class="form-group">
                                    <label class="required-field" name="nombre" for="ticketNameAppliInput">Nombre completo:</label>
                                    <input onchange="validate(this)" maxlength="100" id="ticketNameAppliInput" type="text" name="nombre" class="form-control form-control-user validateable" />
                                    <div class="validation-message">
                                        Verifique la información nuevamente, ingrese el nombre
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="required-field" name="email" for="ticketEmailAppliInput">Email:</label>
                                    <input onchange="validate(this)" maxlength="100" id="ticketEmailAppliInput" type="email" name="email" class="form-control form-control-user validateable" />
                                    <div class="validation-message">
                                        Verifique la información nuevamente, ingrese el correo electronico
                                    </div>
                                </div>

在此处输入图片说明

Jarlik Stepsto

To show a validation message under the field you need a element to display it.

It could be any div, span or whatever you want.

In my example i will use a span to demonstrate how it works:

<input onchange="validate();" type="text" class="validateable" validation-pattern="[0-9]*" />
    <div class="validation-message">Only numbers are allowed in this field!</div>

now in the js code we just have to validate for the pattern and set a input to invalid if it does not match the pattern:

function validate(){
  const validateables = document.getElementsByClassName('validateable');
  Array.prototype.forEach.call(validateables, input => {
        const pattern = input.getAttribute('validation-pattern');
    if(!input.value.match('^' + pattern + '$')){
        input.classList.add('invalid');
    } else {
        input.classList.remove('invalid');
    }
  });
}

and the css to display validation text only if invalid:

.validation-message {
  display: none;
}

input.validateable.invalid + .validation-message{
  display: block;
  color: red;
}

What this code does:

The JS function looks for every input with the class "validateable" and iterates over them. Each element should have an attribute with an validation pattern validation-pattern="[0-9]*" Now the function checks, if the value of the input matches the pattern and add a class invalid to the input or removes it. In the css i defined an invisible div validation-message but if the element bevor this div is an validateable input field, that is invalid, the div will be displayed and you can see the validation message.

Working fidle: https://jsfiddle.net/h687eomf/

UPDATE:

in your case, you just want to validate the field, that you are changing, have a look at my changed example fidle: https://jsfiddle.net/h687eomf/2/

UPDATE 2:

尝试修复您的代码段,假设字段在其值不为空时有效,如果值为空则无效:

function validate(field) {
    const input = field;
    if (!input.value ||  input.value.length === 0) {
        input.classList.add('invalid');      
    } else {
        input.classList.remove('invalid');
    }
}
input {
    display: block;
}

.validation-message {
    display: none;
}

input.validateable.invalid + .validation-message {
    display: block;
    color: red;
}
<div class="form-group">
                                    <label class="required-field" name="matricula" for="ticketIdAppliInput">Matrícula:</label>
                                    <input onchange="validate(this)" maxlength="9" required="required" id="ticketIdAppliInput" type="text" name="matricula" onkeypress="if (isNaN(String.fromCharCode(event.keyCode))) return false;" class="form-control form-control-user validateable"/>
                                    <div class="validation-message">
                                        Verifique la información nuevamente, ingrese la matricula</div>
                                </div>
                                <div class="form-group">
                                    <label class="required-field" name="nombre" for="ticketNameAppliInput">Nombre completo:</label>
                                    <input onchange="validate(this)" maxlength="100" id="ticketNameAppliInput" type="text" name="nombre" class="form-control form-control-user validateable" />
                                    <div class="validation-message">
                                        Verifique la información nuevamente, ingrese el nombre
                                    </div>
                                </div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在颤振中从文本字段中显示验证错误消息

如何在标签中显示文本字段验证错误消息

如何在Vuetify.js的快餐栏中显示文本字段验证检查结果?

颤动:如何在文本字段中显示货币数字

如何在 sencha 的文本字段中显示前导零?

当我接近文本字段时如何在 html 中显示一个小消息框

如何在Codeigniter中显示特定字段的表单验证消息

如何在Yii的文本字段上显示警告消息

在空白文本字段中显示消息错误

如何在文本或文本字段的左侧显示工具提示

如何在Ionic 2的警报对话框中设置警报消息的文本字段?

如何在材质ui中显示比文本字段宽度更长的文本?

如何在PHP表单中验证用户名文本字段

如何在sencha touch中为文本字段执行字母数字验证

如何在Salesforce文本字段中验证电子邮件地址?

如何在标签上显示多个文本字段?

如何在Material ui自动完成的输入文本字段中显示所选值?

如何在 Thymeleaf 中显示从“选择”到“文本字段”的选定项目?

如何在一个文本字段(Google Maps API)中显示2个坐标?

如何在JasperReports的表页脚中通过文本字段显示数据

如何在html表单的输入文本字段中显示响应数据?

如何在Flutter中为文本字段自动显示键盘

如何在循环反应中显示或隐藏特定的文本字段

如何在单独的文本字段或标签中显示我单击的所有按钮的标题

如何在两个字段的验证中显示验证消息并使表格无效

如何在文本字段中添加最少的文本

Flutter-如何在多行文本字段中退出文本字段?

如何在其他文本字段中输入值时清除文本字段

如何在 jetpack compose 中对多文本字段使用文本字段控件?