3

I added field validation attributes like "required" and "pattern" in my form, and the form is inside a ng-controller. The validation works. But it seems the validations are triggered on page load, and I see all the fields are marked as invalid with error message when the page load.

I tried to add "novalidation" attribute to the form as indicated in the examples on AngularJS website, but no luck.

I would like to have the validation triggered the first time the user tries to interact with it. How can I do that?

Update

Here's an example https://jsfiddle.net/davidshen84/00t197gx/

<div class="mdl-cell mdl-cell-6-col mdl-textfield mdl-js-textfield">
  <input class="mdl-textfield__input" type="text" id="screenname" pattern="[a-zA-Z0-9]{3,}" ng-model="comment.screenname" required/>
  <label class="mdl-textfield__label" for="screenname">Screen Name</label>
</div>

On load, you should see all the input fields had a red line under them which indicate they are in the invalid state. And the line turns to blue once validated.

Note: The style on the check button does not work...should not be a concern in the problem.

2
  • could you please add some markup? how you are showing a validation? using ng-messages or showing and hiding divs using ng-show..? Commented Oct 21, 2015 at 1:56
  • this doesn't sound like normal behavior, but without a sample of your code, it's hard to know why this is happening or offer alternatives. Commented Oct 21, 2015 at 1:58

2 Answers 2

2

Angular is going to check the form the same way at any point (load or later) and render the result. If you don't want to display the results on load, add logic to check whether the form has been interacted with. You can hide your error messages using ng-if="yourFormName.$dirty", or display according to the status of an individual field with yourFormName.yourFieldName.$dirty.

Click here for live demo.

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

2 Comments

yeah, I thought about this method. But my situation is a bit more complex because my code mixed with material.js
This won't work if the user simply tabs through the fields as $dirty is only assigned when the control's model is modified.
0

What is currently implemented (wrong IMHO) is that MDL automatically validates input and doesn't mind "novalidate" form attribute. I had to implement check for empty input value (skip validation and remove is-invalid class) and, since angular form validation requires "novalidate" attribute, check:

if (input.form.novalidate = true) // skip validation

that way you can actually turn off mdl validation and leave everything to angular. One more thing is actually required. You can create angular directive which validates expression and add is-invalid class if necessary:

div class="mdl-textfield" mdl-validator="form.email.$error"

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.