如何使用ngPattern验证包含受限字符的输入?

时间:2016-07-26 18:45:01

标签: angularjs ng-pattern angularjs-ng-pattern

我尝试验证受限制字符的输入。输入字段只接受字母,短划线和下划线字符。

当我测试下面的代码时,第一个字符被接受,但第二个字符引发了一个模式错误。

我的RegEx: / ^ [a-zA-Z _-] $ /

HTML code:

<input ng-model="inputText" name="inputText" required md-maxlength="{{options.maxTextLength}}" md-autofocus ng-keypress="keypress($event)" ng-change="textChanged()" ng-pattern='/^[a-zA-Z_-]$/'>
<div ng-messages="inputTextForm.inputText.$error">
    <div ng-message="required">Input is required.</div>
    <div ng-message="md-maxlength">Input has reached the maximum characters allowed.</div>
    <div ng-message="pattern">Invalid characters.</div>
</div>

此代码位于Angular Material Dialog(v1.0.9)

2 个答案:

答案 0 :(得分:1)

对于多个字符,您需要在模式末尾使用<input ... ng-pattern="/^[a-zA-Z_-]*$/" /> <!-- ^ --> <!-- | --> <!-- HERE -------------+ --> ,即:

def conf = new LinkedHashMap()
conf.resourceSrc = "${projectDir}/src/main/j2ee-apps/store.war/resources/"
conf.distFolder = "${conf.resourceSrc}dist"
//for source propertie see https://github.com/eriwen/gradle-js-plugin
conf.js = [
    [
        "name": "store",
        "source": fileTree(dir :"${conf.resourceSrc}/store/js", includes :['**/*.js'])  
    ],
    [
        "name": "lib",
        "source": fileTree(dir :"${conf.resourceSrc}/vendor/js/lib", includes :['**/*.js']) 
    ],
    [
        "name": "vendor",
        "source": fileTree(dir :"${conf.resourceSrc}/vendor/js/modules", includes :['**/*.js']) 
    ]
]

task cleanDist(type: Delete) {
    description 'Clean dist folder'
    delete conf.distFolder
}

task generateTask << {
    conf.js.each { js->
        createCombineTask(js, conf).execute()
        createMinifyJsTask(js, conf).execute()      
    }
}

def createCombineTask(js, conf){
    return tasks.create("combine-${js.name}.js", com.eriwen.gradle.js.tasks.CombineJsTask) {
        source = js.source
        dest = file("${conf.distFolder}/${js.name}.js")
    }
} 

def createMinifyJsTask(js, conf){
    def baseName = "${conf.distFolder}/${js.name}" 

    tasks.create("minify-${js.name}.js", com.eriwen.gradle.js.tasks.MinifyJsTask){
        source = file("${baseName}.js")
        dest = file("${baseName}.min.js")
        sourceMap = file("${baseName}.min.js.map")
        closure {
            warningLevel = 'QUIET'
            compilerOptions.languageIn = 'ECMASCRIPT5'
        }
        doLast {
            def md5 = getFileMD5("${baseName}.min.js", 10);       

            file(dest) << "\n//# sourceMappingURL=${baseName}.min.${md5}.js.map"
            Map srcMap = new JsonSlurper().parseText( sourceMap.text )
            srcMap.sources = ["${baseName}.js"]
            sourceMap.withWriter("UTF-8") { it << new JsonBuilder(srcMap).toPrettyString() }

            def finalFileName = "${baseName}.min.${md5}.js";

            file("${baseName}.min.js").renameTo(file(finalFileName))
            file("${baseName}.min.js.map").renameTo(file("${baseName}.min.${md5}.js.map"))
        }
    }
}

请参阅小提琴:https://jsfiddle.net/5wa5478c/

答案 1 :(得分:0)

有了这个,这个正则表达式表明有效输入是一行(^)的开头,后面是一个批准的字符,后面是行的结尾($)。

删除^和$,它应该可以工作!

ng-pattern='/[a-zA-Z_-]/'

首先在此处测试您的代码:http://rubular.com/