【问题标题】:How to give regex pattern in scope of angular js如何在角度js范围内给出正则表达式模式
【发布时间】:2014-08-09 18:35:46
【问题描述】:

我有一个输入字段,看起来像

 <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://.+;。但这些显示语法错误。我在哪里失踪?

【问题讨论】:

    标签: regex angularjs validation ng-pattern


    【解决方案1】:

    请看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:\/\/.+/;
    

    【讨论】:

    • 对于您的示例中给出的模式,它工作正常。但是对于我的模式,它显示了语法表达式。我错过了任何转义序列吗?
    • 我已经在你的答案中更新了 JS 并标记为答案。我刚刚错过了正斜杠的转义字符。 "$scope.regex = /sftp:\/\/.+/;"
    猜你喜欢
    • 2017-02-28
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2021-08-30
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    相关资源
    最近更新 更多