2 * Copyright 2016 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 $(document).ready(function() {
18 var USER_SERVICE = "/openoapi/auth/v1/users";
19 var $userName = $("#userName");
20 var $password = $("#password");
21 var $cfPsdError = $("#cfPsdError");
22 var $userNameError = $("#userNameError");
23 var $passwordError = $("#passwordError");
25 function initialPage() {
27 $("#confirm").click(function(e) {
28 if (!checkUserRules()) {
31 var data = getCreateUser();
32 createUser(data).done(function() {
33 window.document.location = "/openoui/user/user.html";
36 $("#cancel").click(function(e) {
37 window.document.location = "/openoui/user/user.html";
41 function getCreateUser() {
43 data.userName = $userName.val();
44 data.password = $password.val();
45 data.description = $("#description").val();
46 data.email = "xxxx@xxxx.com";
50 function createUser(data) {
52 url: USER_SERVICE + "?=" + new Date().getTime(),
55 contentType: 'application/json',
57 data: JSON.stringify(data)
61 function checkUserRules() {
62 if (!checkMandatory()) {
66 if (!checkCfPassword()) {
70 if (!checkUserNameRule()) {
74 if (!checkPasswordRule()) {
80 function checkMandatory() {
81 if ($userName.val() == "") {
82 showError($userNameError, "Mandatory.");
86 if ($password.val() == "") {
87 showError($passwordError, "Mandatory.");
93 function checkUserNameRule() {
94 var username = $userName.val();
95 if (!checkLength(5, 30, username)) {
96 showError($userNameError, "The user name length should between 5 and 30.");
100 if (!checkOnlySpecials(username, /[0-9]|[a-z]|[A-Z]|_/g)) {
101 showError($userNameError, "Only Character(a-z\,A-Z\,0-9,_) is allowed.");
105 if(!checkUderScore(username)) {
106 showError($userNameError, 'The character "_" is only allowed in the middle of the user name.');
110 if (!checkNoSpace(username)) {
111 showError($userNameError, "The user name should not contain space.");
118 function checkPasswordRule() {
119 var password = $password.val();
121 if (!checkLength(8, 32, password)) {
122 showError($passwordError, "The password length should between 8 and 32.");
126 if (!checkCotainSpecial(password)) {
127 showError($passwordError, "At least contain: one uppercase letter, one lowercase letter, and one digit, one special character;");
131 if (!checkNoContainAndReverse(password, $userName.val())) {
132 showError($passwordError, "The password should not contain the user name or reverse.");
136 if (!checkNoSpace(password)) {
137 showError($passwordError, "The password should not contain space.");
143 function checkLength(min, max, str) {
144 return str.length >= min && str.length <= max;
147 function checkOnlySpecials(str, reg) {
148 return str.match(reg) && str.match(reg).length == str.length
151 function checkCotainSpecial(password) {
152 return password.match(/\~|\`|\@|\#|\$|\%|\^|\&|\*|\-|\_|\=|\+|\||\?|\/|\(|\)|\<|\>|\[|\]|\{|\}|\"|\,|\.|\;|\'|\!/g) != null
153 && password.match(/[0-9]/g) != null && password.match(/[a-z]/g) != null && password.match(/[A-Z]/g) != null;
156 function checkUderScore(str) {
157 return str.indexOf("_") != 0 && str.lastIndexOf("_") != str.length - 1;
160 function checkNoContainAndReverse(str, str2) {
161 return str.indexOf(str2) == -1 && str.indexOf(str2.split("").reverse().join("")) == -1;
164 function checkNoSpace(str) {
165 return str.indexOf(" ") == -1;
168 function checkCfPassword() {
169 if ($password.val() == $("#cfPassword").val()) {
172 showError($cfPsdError, "The password is not the same.");
176 function showError($Obj, message) {
178 $Obj.css("visibility", "visible");
179 setTimeout(function() {
184 function hideError($Obj) {
185 $Obj.css("visibility", "hidden");