1

I'm having little hard time with a regular expression. Any help would be greatly appreciated.

Problem: I want display an error when user enters drive letters in the path.

for example: if user enters C:\ or D:\ or S:\ .... the form should show an error. If someone enters //remote server/example.txt it should allow them to proceed.

http://plnkr.co/edit/jyKfOdnctnhCkIeT4r2Z?p=preview

<form name='myform'>      
     <input type="text" name='ip' ng-model="usd" ng-pattern="/^[a-zA-Z]:*$/"
     ng-change="convert_to_btc()" placeholder="Enter path"/>
pmh
  • 51
  • 1
  • 7
  • I think you need `ng-pattern="/^(?![a-zA-Z]:\\)/"`, check [this plunkr](http://plnkr.co/edit/3UcFZNsjxPkVXkUJizu2?p=preview). If you need to disallow it not only at the start of the string: `ng-pattern="/^(?!.*\b[a-zA-Z]:\\)/"` – Wiktor Stribiżew Jul 22 '16 at 19:34

1 Answers1

1

The ng-pattern requires a "positive" regex, some pattern that is defining what string is a correct one. So, when you define ^[a-zA-Z]:*$ that means you only allow strings that start with an ASCII letter, then have : zero or more times, up to the end of string.

You need a negative lookahead:

ng-pattern="/^(?![a-zA-Z]:\\)/"
              ^^^           ^

The (?![a-zA-Z]:\\) lookahead fails the match if there is an ASCII letter after the start of the string, followed with a : and then \.

See this plunkr.

If you need to disallow a letter + : + \ not only at the start of the strin use

ng-pattern="/^(?!.*\b[a-zA-Z]:\\)/"
                 ^^
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563