Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularjs Form/Field validation using JavaScript function without directives
    primarykey
    data
    text
    <p>Is there a way to validate a field in angular <strong>without using a directive</strong>? For example: I want to make following validation on an input field.</p> <ul> <li>If field is empty we should show "Field must contain a value" message.</li> <li>if field contains alpha Numeric characters we should show "Field can contain only digits".</li> <li>An EVEN number - message to the user "Value must be an even number".</li> </ul> <p><strong>I want to make following validation in a call to JavaScript function.</strong></p> <p>I googled around and saw that there is a way to use ng-valid and $error , however I was not managed to make it work.</p> <p>Code below is according to one of the answers I got: </p> <pre><code>&lt;div ng-app&gt; &lt;form name='theForm' novalidate&gt; &lt;input type='text' name='theText' ng-model='theText' ng-pattern='/^[0-9]+$/'/&gt; &lt;span ng-show='theForm.theText.$error.pattern'&gt;Field can contain only digits&lt;/span&gt; &lt;span ng-show='theText.length&lt;1'&gt;Field must contain a value&lt;/span&gt; &lt;span ng-show='theText%2!=0&amp;&amp;document.getElementsByName("theText").value!=""&amp;&amp;!theForm.theText.$error.pattern&amp;&amp;!theForm.theText.$pristine'&gt;Value must be an even number&lt;/span&gt; &lt;br/&gt;&lt;input type='submit' value='Submit' /&gt; &lt;/form&gt; </code></pre> <p></p> <p>I want to take what inside the last [span] and put inside a JavaScript function in order to make it more generic and eventually change only JS and not the HTML when conditions are changing</p> <p>Can someone please advise? a working example would be great.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload