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"/>
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]:\\)/"
^^