The function of validation of input fields.

function validateValue() { return { restrict: 'A', require: 'ngModel', link: function(scope, elem, attrs, ctrl) { var regex = new RegExp(/^\d{1,}((\.|,)\d{1,4})?$/); var regexNoLetters = /[a-zA-Zа-я-А-Я]/g; var re = /^(\d{1,5}[.,]\d{4})[\s\S]*/; ctrl.$validators.validateAmount = function(value) { if (ctrl.$viewValue == 0) { return false; } if (value) { var value = value.toString().replace(regexNoLetters, '').replace(',', '.'); var result = value.replace(re, '$1'); ctrl.$setValidity('amount', true); ctrl.$setViewValue(result); if (!regex.test(result)) { ctrl.$setTouched(); } ctrl.$render(); return !value ? true : regex.test(result); } else { return false; } } } }; 

everything works fine in chrome and similar browsers. The problem is safari. There is no way to put "." that is, a fractional number. I can not understand what the problem is. At what in a safari on a poppy 17 "everything is normal. And on 13" a problem. I know that on 13 "there are other jokes too. Can anyone know what the problem is with them? In general, I found out that in safari it does the opposite. Instead of blocking all unnecessary characters," "replace with". "It turns out that." and, "block and the rest of the characters and letters can be entered. What's wrong?

  • Errors / warnings in the browser console there? - Visman
  • no errors and warnings. I beg your pardon. one line was wrong. now corrected. look again. but it was a test code. var regex = new RegExp (/ ^ \ d {1,} ((\. |,) \ d {1,4})? $ /); - Maxim
  • another interesting moment. after 0 "." is put. after any other numbers there is no - Maxim

1 answer 1

The function is working. But. The problem was that the number was in the input type, for the validator to work, the text type of the field is required.

 <input type="text"> 
  • input - non-closing tag - soledar10
  • It turns out that in Safari was not a bug, but a feature? - VostokSisters
  • it turns out so)) - Maxim