source: src/main/resources/static/js/authentication-shared.js@ 43c9090

Last change on this file since 43c9090 was 43c9090, checked in by macagaso <gasoskamarija@…>, 5 weeks ago

Updated version

  • Property mode set to 100644
File size: 2.1 KB
Line 
1function usernameCheck(username){
2 const regex = /^(?=.*[A-Za-z])[A-Za-z0-9]+$/;
3 return regex.test(username);
4}
5function nameSurnameCheck(name,surname){
6 const regex=/^[АБВГДЃЕЖЗЅИЈКЛЉМНЊОПРСТЌУФХЦЧЏШ][абвгдѓежзсијклљмнњопрстќуфхцчџш]+$/;
7 return regex.test(name) && regex.test(surname);
8}
9
10function phoneCheck(phone){
11 const phonePattern = /^07\d\d{3}\d{3}$/;
12 if (!phonePattern.test(phone)) {
13 return false;
14 }
15 else {
16 const extracted=phone.replace(/-/g,"");
17 return extracted.length === 9;
18 }
19}
20function ageCheck(dateOfBirth){
21 const today = new Date();
22 const birthDate = new Date(dateOfBirth);
23 let age = today.getFullYear() - birthDate.getFullYear();
24 const monthDifference = today.getMonth() - birthDate.getMonth();
25 if (monthDifference < 0 || (monthDifference === 0 && today.getDate() < birthDate.getDate())) {
26 age--;
27 }
28 return age >= 18;
29}
30function passwordCheck(password){
31 const regex = /^[A-Za-z0-9!@#$%^&*]{8,}$/;
32 return regex.test(password);
33}
34export async function verificationCheck(userData,condition) {
35 if (!usernameCheck(userData.username)) {
36 alert("Invalid username");
37 return false;
38 }
39 if (!nameSurnameCheck(userData.name, userData.surname)) {
40 alert("Invalid name and surname");
41 return false;
42 }
43 if (!phoneCheck(userData.phone)) {
44 alert("Invalid format of phone");
45 return false;
46 }
47 if (!ageCheck(userData.age)) {
48 alert("Under 18");
49 return false;
50 }
51
52 if (typeof userData.password !== 'undefined') {
53 if (!passwordCheck(userData.password)) {
54 alert("Stronger password");
55 return false;
56 }
57 }
58 if(condition!==false){
59 const response = await fetch(`/api/users/checkDifferentUser`, {
60 method: "POST",
61 headers: {
62 'Content-Type': 'application/json'
63 },
64 body: JSON.stringify(userData)
65 });
66 if (response.ok) {
67 return true;
68 } else {
69 return false;
70 }
71 }
72 return true;
73}
Note: See TracBrowser for help on using the repository browser.