0

I am new to AngularJS. I have developed a simple Angular UI Modal form and I am trying to validate the form on form submission i.e. when end user click the submit button. I followed an video lecture that uses a Angular directive to validate the fields that places 'has-error' class on the parent element. please the images for the clarification.

Angular Form

enter image description here

Error Directive

enter image description here

HTML Template

enter image description here

I want the messages Full Name is required , A valid email is required should only be displayed when either Save button is clicked or the input controls fires up the blur event. please guide what I am missing here....

2 Answers 2

0

Try this one :

E-mail:
<input type="email" ng-model="user.email" name="uEmail" required="" />

<div ng-show="form.$submitted || form.uEmail.$touched">
  <span ng-show="form.uEmail.$error.required">Tell us your email.</span>
  <span ng-show="form.uEmail.$error.email">A valid email.is required</span>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

I would approve your suggestion as an answer. Here I would like to share that I am experiencing a weird behavior of my application which I have understood a bit. Its the blur event in the Error directive for toggling has-error class. There comes a situation when I enter something move and next input, comes back to first one and make it empty then the validation shows up but 'has-error's class is only added when i move to next control i.e. until blur event is fire.
0

Update the ng-if in your error message span tags

ng-if="(employeeForm.fullName.$isDirty || employeeForm.$submitted) && <rest of your validation>"

This will then eval to true if the field has been entered by the user OR the form has been submitted.

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.