0

I am using angularjs to submit a really simple form... I must insert a numeric code in my form. The code must be validate server side, calling an external service. When I get the promise I would like to check either the code was correct or not. If the code is not correct I would like to set a red border in the textbox and show a message... here the html

<input name="code" type="text" style="width:155px;" data-ng-model="Codice" 
    data-ng-required="true" data-ng-pattern="/^(\d){22}$/" data-ng-model-options="{ updateOn: 'mousedown blur' }" />

<span data-ng-show="myform.code.$error.pattern" class="error">Codice non valido</span>

Now I let you see what I do in my controller

if (response.Result == "1")
{
   myform.code.$error.pattern = true;
}

In this way I just see the message but the the border of the textbox remain black... so I have tryed:

if (response.Result == "1")
{
   myform.code.$setValidity('mismatch',false);
}

But in this way, even if I modify the code, the model remain "not valid" so I cannot do a post anymore...

Any solution? Thanx

1 Answer 1

1

In your html create one span for custom validation

 <span class="help-block" ng-show="myform.name.$error.ntValidName">Not valid name</span>

And in controller set the validity like this,if not valid then

 myform.name.$setValidity("ntValidName", false);

This way you can create your custom validation

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

3 Comments

Thank you.. I was wrong in the validation name... in the controller I called it "mismatch".. in the view "pattern"
Ya I saw that But i thought that you html code you are displaying is for first condition and you might have modified your html for 2nd case.
I wanted to use just one span... but think is much better use two different span like you did...

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.