2014-06-19 39 views
2

的範圍正則表達式我有一個輸入的字段,它看起來像如何給在角JS

<input class="inputMargin urlInputWidth" type="text" size="40" name="url" ng-model="user.customerId" maxlength="250" ng-pattern="/sftp://.+/" /> 

它工作正常,並顯示驗證預期。我只是想將這個模式轉移到angularjs的範圍,以獲得更好的形式。我試過這個語法:$scope.sftpValidate="/sftp://.+/";ng-pattern="sftpValidate"。但這並不能證實這種模式。我試圖給出$scope.sftpValidate=/sftp://.+/;$scope.sftpValidate=sftp://.+;。但是這些顯示語法錯誤。我在哪裏失蹤?

回答

3

請參閱JSBin

查看:

<input class="inputMargin urlInputWidth" type="text" size="40" name="url" ng-model="user.customerId" maxlength="250" ng-pattern="regex " /> 

JS

$scope.regex = /sftp://.+/; 

JS更新人短信:

$scope.regex = /sftp:\/\/.+/; 
+1

對於你的榜樣其做工精細給出的格局。但對於我的模式,它顯示的是語法表達式。我是否缺少任何轉義序列? – sms

+0

你可以在這裏測試你的表情http://www.regexr.com/ – sylwester

+1

我已經在你的答案中更新了JS並標記爲答案。我剛剛錯過了正向斜線的轉義字符。 「$ scope.regex = /sftp:\/\/.+/;」 – sms