Update checking comment length.

This commit is contained in:
kunw 2016-12-08 15:57:03 +08:00
parent 6565a3da54
commit 9e73abf8f6
6 changed files with 87 additions and 17 deletions

View File

@ -0,0 +1,71 @@
/*
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
(function() {
'use strict';
angular
.module('harbor.validator')
.directive('charsLength', charsLength);
charsLength.$inject = ['ASCII_CHARS'];
function charsLength(ASCII_CHARS) {
var directive = {
'require': 'ngModel',
'scope': {
min: '@',
max: '@'
},
'link': link
};
return directive;
function link(scope, element, attrs, ctrl) {
ctrl.$validators.charsLength = validator;
function validator(modelValue, viewValue) {
if(ctrl.$isEmpty(modelValue)) {
return true;
}
var actualLength = 0;
if(ASCII_CHARS.test(modelValue)) {
actualLength = modelValue.length;
}else{
for(var i = 0; i < modelValue.length; i++) {
ASCII_CHARS.test(modelValue[i]) ? actualLength += 1 : actualLength += 2;
}
}
if(attrs.min && actualLength < attrs.min) {
return false;
}
if(attrs.max && actualLength > attrs.max) {
return false;
}
return true;
}
}
}
})();

View File

@ -39,14 +39,11 @@
ctrl.$validators.userExists = validator;
function validator(modelValue, viewValue) {
console.log('modelValue:' + modelValue + ', viewValue:' + viewValue);
if(ctrl.$isEmpty(modelValue)) {
console.log('Model value is empty.');
return true;
}
UserExistService(attrs.target, modelValue)
.success(userExistSuccess)
.error(userExistFailed);

View File

@ -21,6 +21,7 @@
.constant('INVALID_CHARS', [",","~","#", "$", "%"])
.constant('PASSWORD_REGEXP', /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,20}$/)
.constant('PROJECT_REGEXP', /^[a-z0-9](?:-*[a-z0-9])*(?:[._][a-z0-9](?:-*[a-z0-9])*)*$/)
.constant('EMAIL_REGEXP', /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
.constant('EMAIL_REGEXP', /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)
.constant('ASCII_CHARS', /^[\000-\177]*$/);
})();

View File

@ -42,11 +42,11 @@
<div class="form-group">
<label for="fullName" class="col-sm-3 control-label">// 'full_name' | tr //:</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="fullName" ng-model="user.realname" name="uFullName" required maxlength="20" invalid-chars>
<input type="text" class="form-control" id="fullName" ng-model="user.realname" name="uFullName" required chars-length max="20" invalid-chars>
<div class="error-message" ng-messages="form.uFullName.$touched && form.uFullName.$error">
<span ng-message="required">// 'full_name_is_required' | tr //</span>
<span ng-message="invalidChars">// 'full_name_contains_illegal_chars' | tr //</span>
<span ng-message="maxlength">// 'full_name_is_too_long' | tr //</span>
<span ng-message="charsLength">// 'full_name_is_too_long' | tr //</span>
</div>
</div>
<div class="col-sm-2">
@ -56,9 +56,9 @@
<div class="form-group">
<label for="comments" class="col-sm-3 control-label">// 'comments' | tr //:</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="comments" ng-model="user.comment" name="uComments" maxlength="20">
<input type="text" class="form-control" id="comments" ng-model="user.comment" name="uComments" chars-length max="20">
<div class="error-message" ng-messages="form.uComments.$touched && form.uComments.$error">
<span ng-show="maxlength">// 'comment_is_too_long' | tr //</span>
<span ng-message="charsLength">// 'comment_is_too_long' | tr //</span>
</div>
</div>
</div>

View File

@ -131,6 +131,7 @@
<script src="/static/resources/js/components/validator/invalid-chars.validator.js"></script>
<script src="/static/resources/js/components/validator/project-name.validator.js"></script>
<script src="/static/resources/js/components/validator/email.validator.js"></script>
<script src="/static/resources/js/components/validator/chars-length.validator.js"></script>
<script src="/static/resources/js/components/search/search.module.js"></script>
<script src="/static/resources/js/components/search/search.directive.js"></script>

View File

@ -30,12 +30,12 @@
<div class="form-group">
<label for="username" class="col-sm-3 control-label">// 'username' | tr //:</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="username" ng-model="user.username" name="uUsername" required maxlength="20" invalid-chars user-exists data-target="username">
<input type="text" class="form-control" id="username" ng-model="user.username" name="uUsername" required invalid-chars user-exists data-target="username" maxlength="20">
<div class="error-message" ng-messages="(form.$submitted || form.uUsername.$touched) && form.uUsername.$error" >
<span ng-message="required">// 'username_is_required' | tr //</span>
<span ng-message="maxlength">// 'username_is_too_long' | tr //</span>
<span ng-message="invalidChars">// 'username_contains_illegal_chars' | tr //</span>
<span ng-message="userExists">// 'username_has_been_taken' | tr //</span>
<span ng-message="invalidChars">// 'username_contains_illegal_chars' | tr //</span>
</div>
</div>
<div class="col-sm-2">
@ -60,11 +60,11 @@
<div class="form-group">
<label for="fullName" class="col-sm-3 control-label">// 'full_name' | tr //:</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="fullName" ng-model="user.fullName" name="uFullName" required maxlength="20" invalid-chars>
<input type="text" class="form-control" id="fullName" ng-model="user.fullName" name="uFullName" required chars-length max="20" invalid-chars>
<div class="error-message" ng-messages="(form.$submitted || form.uFullName.$touched) && form.uFullName.$error">
<span ng-message="required">// 'full_name_is_required' | tr //</span>
<span ng-message="invalidChars">// 'full_name_contains_illegal_chars' | tr //</span>
<span ng-message="maxlength">// 'full_name_is_too_long' | tr //</span>
<span ng-message="charsLength">// 'full_name_is_too_long' | tr //</span>
</div>
<p class="help-block small-size-fonts">// 'full_name_desc' | tr //</p>
</div>
@ -101,9 +101,9 @@
<div class="form-group">
<label for="comments" class="col-sm-3 control-label">// 'comments' | tr //:</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="comments" ng-model="user.comment" name="uComments" maxlength="20">
<input type="text" class="form-control" id="comments" ng-model="user.comment" name="uComments" chars-length max="20">
<div class="error-message" ng-messages="(form.$submitted || form.uComments.$touched) && form.uComments.$error">
<span ng-show="maxlength">// 'comment_is_too_long' | tr //</span>
<span ng-message="charsLength">// 'comment_is_too_long' | tr //</span>
</div>
</div>
</div>