0

I needed angular to validate numbers in scientific notation format within number input fields.

In order to do this I replaced the following constant within angular.js file (line: 18652 v0.13):

var NUMBER_REGEXP = /^\s*(\-|\+)?(\d+|(\d*(\.\d*)))([eE]+[-]?\d+)?\s*$/;
//var NUMBER_REGEXP = /^\s*(\-|\+)?(\d+|(\d*(\.\d*)))\s*$/;

Now values like 0.045e-10 are are accepted as numbers within inputs of the type "number".

I wish to do this from my app instead of from the angular.js file in order to not have to remember to change this again on update of the framework files.

I tried to simply overwrite in the first line of my main app controller like:

NUMBER_REGEXP = /^\s*(\-|\+)?(\d+|(\d*(\.\d*)))([eE]+[-]?\d+)?\s*$/;

But didn't work.

How can this be done?

Al Ex Tsm
  • 2,042
  • 2
  • 29
  • 47

1 Answers1

0

Create a your own validation on the inputs that you want to accept this format. You could create your own constant containing the regex.

module.constant('SCIENTIFIC_NOTATION_NUM_REGEX', /^\s*(\-|\+)?(\d+|(\d*(\.\d*)))([eE]+[-]?\d+)?\s*$/);

Then inject it on any controller or directive that you want to use it and use it on the template.

<input name="scientificNotationNum" ng-pattern="ctrl.SCIENTIFIC_NOTATION_NUM_REGEX">

This example reflect the case when you bind the constant to the controller instance using controllerAs on the view. Other possibility is to bind it to the $scope. Or you could create your own custom input validation to reuse all over your app.

Raulucco
  • 3,406
  • 1
  • 21
  • 27