Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Added Capsule creation form validation for VNC user name and password
Browse files Browse the repository at this point in the history
  • Loading branch information
samithaliyanage committed Jan 4, 2017
1 parent d21aa84 commit 3443a2a
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 40 deletions.
70 changes: 30 additions & 40 deletions app/views/vmcreate.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,48 @@
</select>
</div>
</div>
<div class="form-group">
<div class="form-group has-feedback" id="username-control-group">
<label for="userName" class="col-sm-2 control-label">VNC Login User Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="userName" name="userName" placeholder="User Name" required="true" value="@form.data().get("userName")">
Note: This is different from the login user name of Capsule.
<input type="text" class="form-control" id="userName" name="userName" placeholder="VNC User Name" aria-describedby="user-id-feedback-status" required value="@form.data.get("userName")">
<span id="user-id-feedback" class="form-control-feedback" aria-hidden="true"></span>
<span id="user-id-feedback-status" class="sr-only">(success)</span>
<div id="user_id_alert_placeholder">
</div>
<span class="help-block">
<span id="userid-warn-block" class="warn-block"></span>
Note: This is different from the login user name of Capsule. User Name must be at least 3 characters long and less than 8 characters long. This is due to a VNC limitation.
</span>
</div>
</div>
<div class="form-group">
<div class="form-group has-feedback" id="password-control-group">
<label for="password" class="col-sm-2 control-label">VNC Login Password </label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password" placeholder="Password" required="true">
Note: This is different from the login password of Capsule. Password must be less than 8 characters. This is due to a VNC limitation.
<input type="password" class="form-control" id="password" name="password" placeholder="VNC Password" aria-describedby="password-feedback-status" required>
<span id="password-feedback" class="form-control-feedback" aria-hidden="true"></span>
<span id="password-feedback-status" class="sr-only">(success)</span>
<div id="alert_placeholder">
</div>
<span class="help-block">
<span id="password-warn-block" class="warn-block"></span>
Note: This is different from the login password of Capsule. Password must be at least 3 characters long and less than 8 characters long. This is due to a VNC limitation.
</span>
</div>
</div>
<div class="form-group">
<div class="form-group has-feedback" id="confirm-password-control-group">
<label for="confirmPassword" class="col-sm-2 control-label">Confirm Password [VNC] </label>
<div class="col-sm-10">
<input type="password" class="form-control" id="confirmPassword" name="confirmPassword" placeholder="Password" required="true" oninput="check(this)">
<script language='javascript' type='text/javascript'>
function check (input){
if (input.value != document.getElementById('password').value){
input.setCustomValidity('Password Must be Matching.');
} else {
// input is valid -- reset the error message
input.setCustomValidity('') ;
}
}
</script>
<input type="password" class="form-control" id="confirmPassword" name="confirmPassword" placeholder="VNC Password" aria-describedby="confirm-password-feedback-status" required>
<span id="confirm-password-feedback" class="form-control-feedback" aria-hidden="true"></span>
<span id="confirm-password-feedback-status" class="sr-only">(success)</span>
</div>
</div>
<div class="form-group">
<label for="numberOfVCPUs" class="col-sm-2 control-label">VCPUs</label>
<div class="col-sm-10">

<select id="numberOfVCPUs" name="numberOfVCPUs" class="form-control">
@if(form.data().get("numberOfVCPUs")!= null){
<option selected="selected">
@form.data().get("numberOfVCPUs")
</option>
<option>1</option>
} else{
<option selected="selected">1</option>
}
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
<option>4</option>
Expand All @@ -88,30 +87,21 @@
<label for="memory" class="col-sm-2 control-label">Memory (MB)</label>
<div class="col-sm-10">
<select id="memory" name="memory" class="form-control">
@if(form.data().get("memory")!= null){
<option selected="selected">
@form.data().get("memory")
</option>
<option>1024</option>
<option>2048</option>
}else{
<option>1024</option>
<option selected="selected">
2048
</option>
}
<option selected="selected">2048</option>
<option>4096</option>
<option>8196</option>
<option>8192</option>
<option>16384</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary btn-sm">Create Capsule</button>
<button type="submit" class="btn btn-primary btn-sm" name="createCapsule" id="createCapsule">Create Capsule</button>
</div>
</div>
}
</div>
</div>
</div>
<script type="text/javascript" src="@routes.Assets.at("javascripts/vmcreate.js")"></script>
}
163 changes: 163 additions & 0 deletions public/javascripts/vmcreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
var confirmVNCPasswordMatch = false;
var vncPasswordCorrect = false;
var vncUserIdCorrect = false;

var hasError = function (element, iconElement) {
createCapsuleButtonVisibility();

element.addClass('has-error');
element.removeClass('has-success');
iconElement.addClass('glyphicon');
iconElement.addClass('glyphicon-remove');
iconElement.removeClass('glyphicon-ok');
};

var hasSuccess = function (element, iconElement) {
createCapsuleButtonVisibility();

element.addClass('has-success');
element.removeClass('has-error');
iconElement.addClass('glyphicon');
iconElement.addClass('glyphicon-ok');
iconElement.removeClass('glyphicon-remove');
};

var vncUserIdInputKeyUp = function () {
var userId = $(this).val();
var userIdControlGroup = $('#username-control-group');
var userIdWarnBlock = $('#userid-warn-block');
var userIdFeedback = $('#user-id-feedback');

if (userId.indexOf(' ') >= 0) {
vncUserIdCorrect = false;
hasError(userIdControlGroup, userIdFeedback);
userIdWarnBlock.html('User ID cannot contain white spaces!');
} else if(userId.length == 0){
vncUserIdCorrect = false;
hasError(userIdControlGroup, userIdFeedback);
userIdWarnBlock.html('User ID cannot be empty!');
}else if(userId.length < 3){
vncUserIdCorrect = false;
hasError(userIdControlGroup, userIdFeedback);
userIdWarnBlock.html('User ID must be at least 3 characters long!');
}else if(userId.length > 7){
vncUserIdCorrect = false;
hasError(userIdControlGroup, userIdFeedback);
userIdWarnBlock.html('User ID must be less than 8 characters long!');
}else{
vncUserIdCorrect = true;
hasSuccess(userIdControlGroup, userIdFeedback);
userIdWarnBlock.html('');
}
};

var vncUserIDValidation = function () {
var userId = $("#userName");
var value = "";
userId.bind("change keyup paste" ,vncUserIdInputKeyUp);
};


var checkVNCPasswordStrength = function (password, minPasswordLength, maxPasswordLength) {
if (password.indexOf(' ') >= 0) {
return "SPACE_IN_PASSWORD";
}

if (password.length > maxPasswordLength){
return "PASSWORD_LENGTH_EXCEED_MAX";
}

if (password.length < minPasswordLength){
return "PASSWORD_LENGTH_LESS_THAN_MIN";
}

if (password.length >= minPasswordLength && password.length <= maxPasswordLength) {
return "CORRECT_PASSWORD";
}
};

var matchPasswords = function () {
var passwd = $('#password');
var confirmPasswd = $('#confirmPassword');
var confirmPasswdControlGroup = $('#confirm-password-control-group');
var confirmPasswdFeedback = $('#confirm-password-feedback');

if (passwd.val() !== confirmPasswd.val()) {
confirmVNCPasswordMatch = false;
hasError(confirmPasswdControlGroup, confirmPasswdFeedback);
} else {
confirmVNCPasswordMatch = true;
hasSuccess(confirmPasswdControlGroup, confirmPasswdFeedback);
}
};

var vncPasswordChecker = function () {
var passwd = $('#password');
var confirmPasswd = $('#confirmPassword');
var passwdControlGroup = $('#password-control-group');
var warnBlock = $('#password-warn-block');
var passwordFeedback = $('#password-feedback');
var confirmPasswdControlGroup = $('#confirm-password-control-group');
var confirmPasswdFeedback = $('#confirm-password-feedback');

passwd.keyup(function () {

var passwordStrength = checkVNCPasswordStrength(passwd.val(),3,7);
if (passwordStrength == "SPACE_IN_PASSWORD") {
vncPasswordCorrect = false;
hasError(passwdControlGroup, passwordFeedback);
warnBlock.html('Spaces are not allowed in the password!<br/>');
} else if (passwordStrength == "PASSWORD_LENGTH_EXCEED_MAX") {
vncPasswordCorrect = false;
hasError(passwdControlGroup, passwordFeedback);
warnBlock.html('Password must be less than 8 characters long!<br/>');
} else if (passwordStrength == "PASSWORD_LENGTH_LESS_THAN_MIN") {
vncPasswordCorrect = false;
hasError(passwdControlGroup, passwordFeedback);
warnBlock.html('Password must be at least 3 characters long!<br/>');
} else {
vncPasswordCorrect = true;
hasSuccess(passwdControlGroup, passwordFeedback);
warnBlock.html('');
}
matchPasswords();
});
};


var vncPasswordMatcher = function () {
var passwd = $('#password');
var confirmPasswd = $('#confirmPassword');
var confirmPasswdControlGroup = $('#confirm-password-control-group');
var confirmPasswdFeedback = $('#confirm-password-feedback');

confirmPasswd.keyup(matchPasswords);
};

var createCapsuleButtonVisibility = function () {
var createCapsuleButton = $('#createCapsule');

// disable submit button at start
createCapsuleButton.prop("disabled",true);

if (confirmVNCPasswordMatch && vncPasswordCorrect && vncUserIdCorrect) {
createCapsuleButton.prop("disabled",false);
}else{
createCapsuleButton.prop("disabled",true);
}
};

var triggerAlreadyFilledFields = function() {
$('input').each(function() {
var elem = $(this);
if (elem.val()) elem.change();
})
};

$(document).ready(function(){
createCapsuleButtonVisibility();
vncUserIDValidation();
vncPasswordChecker();
vncPasswordMatcher();
setTimeout(triggerAlreadyFilledFields, 250);
});

0 comments on commit 3443a2a

Please sign in to comment.