3

I am having hard time finding out how can I get AngularJS not to ignore whitespaces in strings. Im trying to validate password requirements with AngularJS directive and regex pattern and problem is that I dont want to allow leading or trailing whitespace in password.

Here is fiddle http://jsfiddle.net/GX5Kr/9/

var regex = /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]{5,})$/;
makallio85
  • 1,366
  • 2
  • 14
  • 29
  • `[SPACE][SPACE][SPACE][SPACE][SPACE]foo[SPACE][SPACE][SPACE][SPACE][SPACE]` reports length `3` instead of `13` - are you sure it doesn't `trim` the output? – h2ooooooo Jan 16 '14 at 18:47
  • No trimming in fiddle – makallio85 Jan 16 '14 at 18:49
  • There is nothing in the regex that allows for leading/trailing spaces since the final constraint is `[a-zA-Z0-9]{5,}` and its all wrapped with ^$ anchors. Must be something else. –  Jan 16 '14 at 18:54
  • I DONT want to allow whitespaces. – makallio85 Jan 16 '14 at 18:59
  • That regex will NEVER match a whitespace. –  Jan 16 '14 at 19:03
  • 1
    Yes. That is not my problem. Problem is angularjs striping all whitespaces off from variables. That way i cant never check them. See fiddle before commenting... – makallio85 Jan 16 '14 at 19:05
  • I tried to go to the fiddle, but my browser is too old, not supported. –  Jan 16 '14 at 19:16
  • possible duplicate of [Get trailing white space from input type password to scope?](http://stackoverflow.com/questions/17621472/get-trailing-white-space-from-input-type-password-to-scope) – C Fairweather Jan 16 '14 at 19:27

1 Answers1

8

This question is a duplicate of Get trailing white space from input type password to scope?

You're probably looking for ng-trim="false". This will update your model including spaces typed into your <input>.

I updated your fiddle for proof. http://jsfiddle.net/GX5Kr/10/

C Fairweather
  • 696
  • 4
  • 20