简体   繁体   English

Angular JS textarea验证

[英]Angular JS textarea validation

I have this AngularJS code trying to show two stars next to label when there is no text in the textarea. 我有这个AngularJS代码试图在textarea中没有文本时显示标签旁边的两颗星。 Same code works for input tags, but not with textarea. 相同的代码适用于输入标记,但不适用于textarea。

<div class="input-left">
    <label for="email">
        <span ng-show="contactform.email.$error.required" class="required">*</span>Email:
    </label>
    <input ng-model="email" type="text" name="email" id="email" required></br></br>
    <label for="budget">Budzet:</label>
    <input ng-model="budget" type="text" name="budget" id="budget">
</div>
<div class="clearboth">
    <label for="msg" class="left" >
        <span ng-show="contactform.msg.$error.required" class="required">**</span>Pitanja ili Komentari?
    </label>
    <textarea ng-model="msg" rows="8" cols="50" class="input-no-width rounded shaded left clearboth" id="msg" required></textarea>
</div>

According to AngularJS documentation - textarea should behave same as input. 根据AngularJS文档 - textarea的行为与输入相同。

Your problem with the <textarea> tag is that it doesn't define name attribute. 您对<textarea>标记的问题是它没有定义name属性。

AngularJS uses the name attribute to expose validation errors. AngularJS使用name属性来公开验证错误。

You should define your textarea like so: 你应该像这样定义你的textarea:

<textarea ng-model="msg" name="msg" rows="8" cols="50"
          class="input-no-width rounded shaded left clearboth" id="msg" required>
</textarea>

Please note that having id and ng-model is not enough to properly handle validation messages. 请注意,使用idng-model不足以正确处理验证消息。 In AngularJS applications the id attribute often doesn't serve much purpose and could be omitted. 在AngularJS应用程序中, id属性通常不起作用,可以省略。

For form validation name is very important. 对于表单验证, 名称非常重要。 Give it a name it should work. 给它一个应该起作用的名字。

<textarea ng-model="msg" 
          rows="8" 
          cols="50" 
          class="input-no-width rounded shaded left clearboth" 
          id="msg" 
          name="msg" 
          required>
</textarea>

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

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