0

Eg. assume that we have form with zip-code field. Requirement is that max-length is 10, required, and for some counteries we use some regexp... So max-length and required is easy but what about zip-code pattern? We have another select where user checks country... How to validate zip-code filed based on value of antoher field in best way? :)

of course we could write directive and in it's scope watch for changes in model of countries and validate code when it changes but if there is more elegant way to do this?

1 Answer 1

1

Assume that your data about countries and regex of zip codes are stored in this way:

$scope.countries = [ {name:'country1', zip:'zipRegex1'}, {name:'country2', zip:'zipRegex2'} ]; 

Assume that you have this dropdown for countries:

<select ng-model="selectedCountry" ng-options="country.name for country in countries"></select> 

You can have input with ng-pattern like this:

<input type="text" ng-model="zipFromUser" ng-pattern="selectedCountry.zip" required> 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes it is good solution but lets assume that we cannot have zip assigned to country (because country list comes from rest service). And what if country doesn't have zip code regexp?
So get this list coming from restful service and add to it new field (zip code regexp) with javascript where it is is needed. Where it is not, just add .* pattern (.* matches any symbols)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.