14

I have this AngularJS code trying to show two stars next to label when there is no text in the textarea. Same code works for input tags, but not with 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.

2 Answers 2

27

Your problem with the <textarea> tag is that it doesn't define name attribute.

AngularJS uses the name attribute to expose validation errors.

You should define your textarea like so:

<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. In AngularJS applications the id attribute often doesn't serve much purpose and could be omitted.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I was thinking it uses id.
This helped me a lot. But I think there is something weird going on at the same time. I can leave out the name attribute on a text input field and the validation will work just fine, but the textarea require me to have it there.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.