1

I've a form control where user has to enter Id. I've validated submit button to enable only when field is filled with value(only numbers).

<input type="text" ng-model="Movie_Id" name="Movid" ng-pattern="onlyNumbers" required/>
<input type="submit" value="Add" ng-disabled="adduserform.$invalid" data-ng-click="Save()"/>

But I want to notify user dynamically when he tries to enter alphabets like "You should only enter Numbers" because user may not know the reason why Submit button is disabled when he enters alphabets.

1

1 Answer 1

1

For this purpose you could use $error property of form of field level $error property.

<form name="adduserform">
  <input type="text" ng-model="Movie_Id" name="Movid" ng-pattern="onlyNumbers" required/>
  <span ng-show="adduserform.Movid.$invalid"> Only number are allowed</span>
  <input type="submit" value="Add" ng-disabled="adduserform.$invalid" data-ng-click="Save()"/>
</form>

In above example I had used adduserform.Movid.$invalid which will tell us that Movid is valid field or not, & if it doesn't valid then validation message will shown.

Update

As we want two different errors to be show we could use $error.required to get required error & $error.pattern to get patterns match or not

Markup

<span style="color:red" ng-show="adduserform.Movid.$dirty && adduserform.Movid.$invalid"> <br /> 
  <span ng-show="adduserform.Movid.$error.required" class="spnmsg">Field must not be empty</span> 
  <span ng-show="adduserform.Movid.$error.pattern">Only Numbers</span> 
</span>

Working Plunkr

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

5 Comments

thnks for ur code.......but the scenario is quite like 1). If user let control empty then "Field must not be empty" should appear 2). if user enters alphabets then above error should disappear n "Only numbers" have to be shown. Its like 1 & 2 should only appear based on input from user..... <input type="text" ng-model="Movie_Id" class="form-control" name="Movid" placeholder="Movie Id" ng-pattern="onlyNumbers" required title="Enter Movie Id" />
<span style="color:red" ng-show="adduserform.Movid.$dirty && adduserform.Movid.$invalid"> <br /> <span ng-show="adduserform.Movid.$error.required" class="spnmsg">Field must not be empty</span> <span ng-show="adduserform.Movid.$invalid">Only Numbers</span> </span> But here both errors are appearing together...how could i overcome it....
@VinoSpartan you number error should look at adduserform.Movid.$error.pattern will show an error, see my updated answer with plunkr
thnks a lot bro......Magic works......how could i be in touch as i'm building a page with angular........as new one, quite need guidance......
@VinoSpartan go through this johnpapa.net/spa & please do upvote if it does helped you, Thanks :-)

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.