简体   繁体   English

自定义错误验证消息未显示

[英]Custom Error Validation Message Not Showing

I am doing error validation in Angular as follows. 我在Angular中进行错误验证,如下所示。

<select ng-model = "color" class = "form-control"
    ng-options = "color as color for color in colors" required>
    <option value="">Choose an option</option> 
</select>

<span ng-show="serviceForm.color.$error.required"> My custom error message </span>

The error validation shows up as expected, however, the custom message does not. 错误验证将按预期方式显示,但是自定义消息不会显示。 Instead, I get the following. 相反,我得到以下内容。

在此处输入图片说明

This looks nice but how do I replace it with my custom message? 这看起来不错,但是如何用自定义消息替换呢? It'd be cool if I know where that is coming from so I can edit it. 如果我知道它来自哪里,那就太好了,我可以对其进行编辑。

In addition, my custom message appears if I take out 另外,如果我取出,我的自定义消息也会出现

<option value="">Choose an option</option> 

and then it always appears. 然后它总是出现。 Why is this? 为什么是这样?

This message most probably comes from your browser as it does form validaten . 该消息很可能来自您的浏览器,因为它确实是validaten形式

You have to add the novalidate attribute to your form to disable it: 您必须将novalidate属性添加到表单中以将其禁用:

<form novalidate>
<select ng-model = "color" class = "form-control" name="lstColor"
    ng-options = "color as color for color in colors" data-msg-required="Please select color from list" required>
    <option value="">Choose an option</option> 
</select>

Try above and hope will solve your problem 尝试以上,希望能解决您的问题

<form novalidate> can be used to avoid these messages. <form novalidate>可用于避免这些消息。

But you can also use setCustomValidity function when oninvalid event occurs to replace it with your custom message. 但是,当发生oninvalid事件时,也可以使用setCustomValidity函数将其替换为自定义消息。

Like: 喜欢:

<input class="form-control" type="email"  required="" placeholder="username" oninvalid="this.setCustomValidity('Please Enter valid email')"></input>

Helpful explanation here . 有用的解释在这里

Working Plnkr 工作Plnkr

You can replace HTML5 validation message of that specific field with space on form submit. 您可以将该特定字段的HTML5验证消息替换为表单提交上的space It will prevent displaying the message and will not affect other HTML5 validation message. 这将阻止显示该消息,并且不会影响其他HTML5验证消息。

JavaScript: JavaScript的:

$scope.ValidateInput = function(){
    var email = document.getElementById("colorId");

    if (email.validity.valueMissing) {
        email.setCustomValidity(" ");
    } else {
        email.setCustomValidity("");
    }
  }

HTML : HTML

<form name='serviceForm'>
      <select name='color' id='colorId' ng-model = "color" class = "form-control"
    ng-options = "color as color for color in colors" required>
         <option value="">Choose an option</option> 
      </select>

     <span ng-show="serviceForm.color.$error.required"> My custom error message </span>
     <input type='submit' ng-click='ValidateInput()' value='Submit'>
</form>

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

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