Skip to content

Commit

Permalink
add user information input validation
Browse files Browse the repository at this point in the history
Signed-off-by: cbh778899 <[email protected]>
  • Loading branch information
cbh778899 committed Jul 15, 2024
1 parent bb465c5 commit d3dd5f6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
23 changes: 21 additions & 2 deletions components/account-page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import capitalizeFirstLetter from "../../tools/capitalizeFirstLetter.js";
import getSVG from "../../tools/svgs.js";
import showMessage from "../../tools/message.js";
import createDialog from '../../tools/dialog.js';
import { email_not_valid, password_not_valid, username_not_valid, validateEmail, validatePassword, validateUsername } from '../../tools/validators.js';

let input_details_main = null, init = false, toggleDialog;
let current_user = {}, isRegister = false, user_info_saved = localStorage.getItem('saved-user-login-info');
Expand Down Expand Up @@ -127,11 +128,18 @@ function submitDetails(evt) {
const repeat_new_password = evt.target['repeat-new-password'].value;
const submit_values = {}
if(email && email !== current_user.email) {
if(!validateEmail(email)) {
showMessage(email_not_valid, { type: "error" })
return;
}
submit_values.email = email;
}
if(new_password) {
if(repeat_new_password !== new_password) {
showMessage("Passwords are not same!", { type: 'err' })
if(!validatePassword(new_password)) {
showMessage(password_not_valid, { type: 'err' })
return;
} else if(repeat_new_password !== new_password) {
showMessage("Passwords are not same!", { type: 'err' })
return;
}
submit_values.password = new_password;
Expand All @@ -152,6 +160,17 @@ function submitDetails(evt) {
const keep_login = evt.target['keep-login'].checked;
if(isRegister) {
const email = evt.target.email.value;
if(!validateUsername(username)) {
showMessage(username_not_valid, { type: 'err' })
return;
} else if(!validateEmail(email)) {
showMessage(email_not_valid, { type: 'err' })
return;
} else if(!validatePassword(password)) {
showMessage(password_not_valid, { type: 'err' })
return;
}

const repeat_password = evt.target['repeat-password'].value;

if(password !== repeat_password) {
Expand Down
21 changes: 21 additions & 0 deletions tools/validators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const email_not_valid = 'Email pattern not valid!';
export const password_not_valid = 'Password should have at least 8 characters combines capital letters, lowercase letters, numbers and special characters.';
export const username_not_valid = 'Username should includes only letters, numbers, -, _ and white space, no less than 4 characters and no more than 20 characters.';

export function validateEmail(string) {
return (
/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(string)
)
}

export function validatePassword(string) {
return (
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()-_+=])[a-zA-Z\d!@#$%^&*()-_+=]{8,}$/.test(string)

Check warning

Code scanning / CodeQL

Overly permissive regular expression range Medium

Suspicious character range that is equivalent to \[)*+,\-.\/0-9:;<=>?@A-Z\\[\\\\]^_\].

Check warning

Code scanning / CodeQL

Overly permissive regular expression range Medium

Suspicious character range that overlaps with \d in the same character class, and overlaps with A-Z in the same character class, and is equivalent to \[)*+,\-.\/0-9:;<=>?@A-Z\\[\\\\]^_\].
)
}

export function validateUsername(string) {
return (
/^[a-zA-Z0-9-_ ]{4,20}$/.test(string)
)
}

0 comments on commit d3dd5f6

Please sign in to comment.