In order to validate the input field in accordance with the user's needs using AngularJS, I have shared my code below:
<div ng-class="{ 'myError': billdata.longitude.$touched && billdata.longitude.$invalid }">
<input type="text" name="longitude" id="longitude" class="form-control oditek-form" placeholder="Add Longitude coordinate" ng-model="longitude" ng-pattern="/^[0-9]+([,.][0-9]+)?$/" ng-keypress="clearField('businessno');">
</div>
<div class="help-block" ng-messages="billdata.longitude.$error" ng-if="billdata.longitude.$touched">
<p ng-message="pattern" style="color:#F00;">This field only allows numbers (e.g. 0,1..9).</p>
</div>
The requirement here is that users should be able to enter numbers only with a +
or -
, such as +123.45 or -095.4567
. However, currently, the use of +
or -
is not allowed, only numbers are permitted.