Search code examples
angularjsng-pattern

Validating character string with ng-pattern


I want an ng-pattern expression to validate a textbox in AngularJS JavaScript. Contents for this textbox must have at least one Alphabetic character, but may also contain any number of Numeric and/or Special characters.

Examples:

  1. a1234$ It is acceptable (because 'a' is present)
  2. 123% It is not acceptable
  3. abc It is acceptable
  4. 56456a It is acceptable
  5. 123$%^abc It is acceptable

Solution

  • Use ng-pattern like this.

    <input type="text" ng-model="price" name="price_field" ng-pattern="/.*[a-zA-Z]+.*/" required>