0

I am having trouble displaying custom message upon validation in my AngularJS app.

Below is my code:

<div class="row">
    <div class="input-field col s12">
       <input id="description" rows="3" class="materialize-textarea no-pad-bot no-pad-top" ng-model="Description" ng-required="isValidationRequired()" ng-minlength="5"></input>
        <span class="help-inline" ng-show="submitted && frm1.description.$error.required">Comments are required</span>

     </div>
 </div>

 <input type="submit" class="white-text waves-effect btn" value="Enter" ng-click="submitted=true" />

The above works but the message displayed is "Please fill out this field". Which is not the message I specified. I want my own custom message displayed. I cannot find anything wrong with my code. Can anyone help point me in the right direction.

2 Answers 2

1

There are a few things wrong with your code.

  1. Form isn't included, I am assuming you just didn't include that bit of code in this post.

  2. You need to specify the name of the input. So when doing frm1.description, it knows what description is.

  3. Make sure the function isValidationRequired returns true

  4. You need to include novalidate in the form tag

Here, I have a working example

  <form novalidate name="frm1">
<div class="row">
<div class="input-field col s12">
   <input name="description" id="description" rows="3" class="materialize-textarea no-pad-bot no-pad-top" ng-model="Description" ng-required="isValidationRequired()" ng-minlength="5"></input>
    <div class="help-inline" ng-show="submitted && frm1.description.$error.required">Comments are required</div>

 </div>

http://jsfiddle.net/mlhuff12/Lvc0u55v/4503/

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

2 Comments

1. Form is there, I just did not include it. 2. I specified the id. 3. Yes, function returns true 4. When I include novalidate my validation stops working
I think you are onto something here. I changed id attribute to name and my validation showed up. Also added novalidate. Thanks. You rock!
0

Ur getting a message you did code? Sounds like a browser validation. Check your statement and add "novalidate" to it. I.e.

<form novalidate>

Maybe this helps...

3 Comments

what will it do? And no, I am not getting the message I coded, I am getting the browser messsage
@ElenaDBA "novalidate is used to disable browser's native form validation" -docs.angularjs.org/guide/forms
I tried that and it broke my validation. The thing is I do want to validate but I want it to display custom error message

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.