source: src/main/resources/static/js/authentication-shared.js@ 743de55

Last change on this file since 743de55 was 743de55, checked in by macagaso <gasoskamarija@…>, 6 weeks ago

Initial commit

  • Property mode set to 100644
File size: 2.0 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) {
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 const response = await fetch(`/api/users/checkDifferentUser`, {
59 method: "POST",
60 headers: {
61 'Content-Type': 'application/json'
62 },
63 body: JSON.stringify(userData)
64 });
65 if (response.ok) {
66 return true;
67 } else {
68 return false;
69 }
70
71}
Note: See TracBrowser for help on using the repository browser.