简体   繁体   中英

Angularjs ng-pattern not working with textarea

I have a textarea and i need to validate input that it only allow digits hyphen space and dot entered. However tried ng-pattern and it didnt work with simple example. changed the pattern multiple times and it looks like as if ng-pattern is not there or not getting invoked. any suggestions?

<textarea id="field"  name="ranges"
        ng-model="editData.ranges"
        style="height: 120px;max-width: 400px;"
        class="form-control" ng-pattern="/^\d+$/">
</textarea>

The code seems fine but since you did not provide your entire code I cannot say much..

But do have a look at the following link which is working fine

http://jsfiddle.net/u1fm0jLo/128/

    <textarea type="text" name="phone" ng-model="phone" 
         ng-pattern="/^\d+$/" maxlength="12" 
      placeholder="555-555-5555"></textarea>

Your regex doesn't check for all of the characters you listed. You'll want to watch the $valid or $error properties of either the form object or formobject.fieldname

<div ng-app>
    <div ng-controller="PatternTestCtrl">
        <form name="ngform">
            <textarea type="text" name="range" ng-model="editData.ranges" ng-pattern="/^[\d+\-\.\s]*$/" maxlength="12"></textarea>
        </form>
        form valid? {{ngform.$valid}}<br />
        field error: {{ngform.range.$error}}<br />
        field valid? {{ngform.range.$valid}}

    </div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM