From a85e0de3aa1766c9ac0778191b4e9760cb49e78b Mon Sep 17 00:00:00 2001 From: Dakshika Jayathilaka Date: Thu, 17 Dec 2015 16:36:40 +0530 Subject: [PATCH] rewrite user create logic with jquery validation lib --- .../public/js/validate-register.js | 189 +++++++----------- .../units/iot.unit.user.register/register.hbs | 69 +++---- 2 files changed, 105 insertions(+), 153 deletions(-) diff --git a/features/device-mgt-iot-feature/org.wso2.carbon.device.mgt.iot.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.user.register/public/js/validate-register.js b/features/device-mgt-iot-feature/org.wso2.carbon.device.mgt.iot.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.user.register/public/js/validate-register.js index df42e7c09a..501a07678c 100644 --- a/features/device-mgt-iot-feature/org.wso2.carbon.device.mgt.iot.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.user.register/public/js/validate-register.js +++ b/features/device-mgt-iot-feature/org.wso2.carbon.device.mgt.iot.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.user.register/public/js/validate-register.js @@ -1,100 +1,64 @@ -var emailIsValid = function (email) { - var atPosition = email.indexOf("@"); - var dotPosition = email.lastIndexOf("."); - return !(atPosition < 1 || ( dotPosition - atPosition < 2 )); -}; - -var validatePassword = function (psswd, conPass) { - var error = ""; - var illegalChars = /[\W_]/; // allow only letters and numbers - - if ((psswd.length < 5) || (psswd.length > 15)) { - error = "The password is of wrong length. Should be between 5 and 15 characters. \n"; - $('.wr-validation-summary strong').text(error); - $('.wr-validation-summary').removeClass("hidden"); - return false; - - } else if (illegalChars.test(psswd)) { - error = "The password contains illegal characters.\n"; - $('.wr-validation-summary strong').text(error); - $('.wr-validation-summary').removeClass("hidden"); - return false; - - } else if ((psswd.search(/[a-zA-Z]+/) == -1) || (psswd.search(/[0-9]+/) == -1)) { - error = "The password must contain at least one numeral and one character.\n"; - $('.wr-validation-summary strong').text(error); - $('.wr-validation-summary').removeClass("hidden"); - return false; - - } else if (psswd != conPass) { - error = "The password and confirm-password should match.\n"; - $('.wr-validation-summary strong').text(error); - $('.wr-validation-summary').removeClass("hidden"); - // return false; - - } else { - return true; - } - -}; - -$(function () { - $("button#add-user-btn").click(function () { - //e.preventDefault(); - var username = $("input#user_name").val(); - var firstname = $("input#first_name").val(); - var lastname = $("input#last_name").val(); - var emailAddress = $("input#email").val(); - var password = $("input#password").val(); - var passwordConfirm = $("input#password_confirmation").val(); - - // var userRoles = $("select#roles").val(); - - if (!firstname) { - $('.wr-validation-summary strong').text("Firstname is a required field. It cannot be empty."); - $('.wr-validation-summary').removeClass("hidden"); - return false; - } else if (!lastname) { - $('.wr-validation-summary strong').text("Lastname is a required field. It cannot be empty."); - $('.wr-validation-summary').removeClass("hidden"); - return false; - } else if (!username) { - $('span.wr-validation-summary strong').text("Username is a required field. It cannot be empty."); - $('.wr-validation-summary').removeClass("hidden"); - return false; - } else if (!emailAddress) { - $('.wr-validation-summary strong').text("Email is a required field. It cannot be empty."); - $('.wr-validation-summary').removeClass("hidden"); - return false; - } else if (!emailIsValid(emailAddress)) { - $('.wr-validation-summary strong').text("Email is not valid. Please enter a correct email address."); - $('.wr-validation-summary').removeClass("hidden"); - return false; - } else if (!password) { - $('.wr-validation-summary strong').text("Please enter a user login password."); - $('.wr-validation-summary').removeClass("hidden"); - return false; - } else if (!passwordConfirm) { - $('.wr-validation-summary strong').text("Please re-type password"); - $('.wr-validation-summary').removeClass("hidden"); - return false; - } else if (!validatePassword(password, passwordConfirm)) { - return false; - } else if (!$('#t_and_c').is(':checked')) { - $('.wr-validation-summary strong').text("Please accept our terms and conditions"); - $('.wr-validation-summary').removeClass("hidden"); - } else { - +$(document).ready(function(){ + $("#registerForm").validate({ + onfocusout: false, + rules: { + first_name: { + required: true + }, + last_name: { + required: true + }, + user_name: { + required: true, + minlength: 3 + }, + email: { + required: true, + email: true + }, + password: { + required: true, + minlength: 5 + }, + password_confirmation: { + required: true, + equalTo: "#password" + } + }, + messages: { + first_name: { + required: "Firstname is a required field. This cannot be empty." + }, + last_name: { + required: "Lastname is a required field. This cannot be empty." + }, + user_name: { + required: "Username is a required field. This cannot be empty.", + minlength: "" + }, + email: { + required: "Email is a required field. This cannot be empty.", + email: "Email is not valid. Please enter a correct email address." + }, + password: { + required: "Please enter a user login password", + minlength: "Password should be between 5 and 30 characters." + }, + password_confirmation: { + required: "Please enter a user login password", + equalTo: "Please enter the same password as above" + } + }, + submitHandler: function(form) { var addUserFormData = {}; - addUserFormData.username = username; - addUserFormData.firstname = firstname; - addUserFormData.lastname = lastname; - addUserFormData.emailAddress = emailAddress; - addUserFormData.password = password; + addUserFormData.username = $("input#user_name").val(); + addUserFormData.firstname = $("input#first_name").val(); + addUserFormData.lastname = $("input#last_name").val(); + addUserFormData.emailAddress = $("input#email").val(); + addUserFormData.password = $("input#password").val(); addUserFormData.userRoles = null; var context = $(".form-login-box").attr("action"); - var addUserAPI = context + "/api/user/register"; $.ajax({ @@ -104,42 +68,39 @@ $(function () { data: JSON.stringify(addUserFormData), success: function (data) { if (data == 200) { - $('.wr-validation-summary strong').text("Successfully Submitted."); - $('.wr-validation-summary strong').removeClass("label-danger"); - $('.wr-validation-summary strong').addClass("label-success"); + $('.wr-validation-summary strong').html(" Successfully Submitted."); + $('.wr-validation-summary').removeClass("alert-danger"); + $('.wr-validation-summary').addClass("alert-success"); } else if (data == 201) { - $('.wr-validation-summary strong').text("User created. You will be redirected to login page"); - $('.wr-validation-summary strong').removeClass("label-danger"); - $('.wr-validation-summary strong').addClass("label-success"); + $('.wr-validation-summary strong').html(" User created " + + "succssfully. You will be redirected to login page."); + $('.wr-validation-summary').removeClass("alert-danger"); + $('.wr-validation-summary').addClass("alert-success"); setTimeout(function () { window.location = context + "/login"; }, 2000); } else if (data == 400) { - $('.wr-validation-summary strong').text("Exception at backend."); - $('.wr-validation-summary strong').removeClass("label-danger"); - $('.wr-validation-summary strong').addClass("label-warning"); + $('.wr-validation-summary strong').html(" Exception at backend."); + $('.wr-validation-summary').removeClass("alert-danger"); + $('.wr-validation-summary').addClass("alert-warning"); } else if (data == 403) { - $('.wr-validation-summary strong').text("Action not permitted."); + $('.wr-validation-summary strong').html("Action not permitted."); } else if (data == 409) { - $('.wr-validation-summary strong').text("User exists."); - $('.wr-validation-summary strong').removeClass("label-default"); - $('.wr-validation-summary strong').addClass("label-success"); + $('.wr-validation-summary strong').html(" User name already exists."); + $('.wr-validation-summary').removeClass("alert-default"); + $('.wr-validation-summary').addClass("alert-success"); } $('.wr-validation-summary').removeClass("hidden"); $('#password').val(''); $('#password_confirmation').val(''); - // return true; }, error: function (err) { - $('.wr-validation-summary strong').text("An unexpected error occurred."); + $('.wr-validation-summary strong').html(" An unexpected error occurred."); $('.wr-validation-summary').removeClass("hidden"); - return false; + return false; } }); } - return false; - }); -}); - - + }); +}); \ No newline at end of file diff --git a/features/device-mgt-iot-feature/org.wso2.carbon.device.mgt.iot.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.user.register/register.hbs b/features/device-mgt-iot-feature/org.wso2.carbon.device.mgt.iot.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.user.register/register.hbs index 4cf428d124..cfc8f4ce4a 100644 --- a/features/device-mgt-iot-feature/org.wso2.carbon.device.mgt.iot.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.user.register/register.hbs +++ b/features/device-mgt-iot-feature/org.wso2.carbon.device.mgt.iot.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.user.register/register.hbs @@ -1,61 +1,52 @@ +{{unit "uuf.unit.lib.form-validation"}}

Register

-

Create new account on IoT Server

- - - - +

Create new account on IoT Server (All fields are required.)


- -