1

I'm trying to validate my form. In one field, I have to validate it null & it's value. How can I do this?

By the way, I don't write a function for this on controller.

<form name="frm">
<input type="text" ng-model="myModel" ng-required="myModel != '' || myModel != undefined || myModel != 'xxx'" />
<button ng-disabled="frm.$invalid" >Send</button>

1 Answer 1

1

You can't do it with ng-required only because it checks if the value was entered or not. In your case you can use ng-required to cover this statements: myModel != '' || myModel != undefined. And if you want to check that myModel value isn't 'xxx' you should use ng-pattern directive. Appropriate regex for your case should be: /^(xxx.+|(?!xxx).*)$/.

<form name="frm">
   <input type="text" ng-model="myModel" ng-pattern="/^(xxx.+|(?!xxx).*)$/" ng-required="" />
   <button ng-disabled="frm.$invalid" >Send</button>
</form>

Check documentation about directives components for input.

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

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.