[743de55] | 1 | function usernameCheck(username){
|
---|
| 2 | const regex = /^(?=.*[A-Za-z])[A-Za-z0-9]+$/;
|
---|
| 3 | return regex.test(username);
|
---|
| 4 | }
|
---|
| 5 | function nameSurnameCheck(name,surname){
|
---|
| 6 | const regex=/^[АБВГДЃЕЖЗЅИЈКЛЉМНЊОПРСТЌУФХЦЧЏШ][абвгдѓежзсијклљмнњопрстќуфхцчџш]+$/;
|
---|
| 7 | return regex.test(name) && regex.test(surname);
|
---|
| 8 | }
|
---|
| 9 |
|
---|
| 10 | function phoneCheck(phone){
|
---|
[43c9090] | 11 | const phonePattern = /^07\d\d{3}\d{3}$/;
|
---|
[743de55] | 12 | if (!phonePattern.test(phone)) {
|
---|
| 13 | return false;
|
---|
| 14 | }
|
---|
| 15 | else {
|
---|
| 16 | const extracted=phone.replace(/-/g,"");
|
---|
| 17 | return extracted.length === 9;
|
---|
| 18 | }
|
---|
| 19 | }
|
---|
| 20 | function 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 | }
|
---|
| 30 | function passwordCheck(password){
|
---|
| 31 | const regex = /^[A-Za-z0-9!@#$%^&*]{8,}$/;
|
---|
| 32 | return regex.test(password);
|
---|
| 33 | }
|
---|
[43c9090] | 34 | export async function verificationCheck(userData,condition) {
|
---|
[743de55] | 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 | }
|
---|
[43c9090] | 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 | }
|
---|
[743de55] | 71 | }
|
---|
[43c9090] | 72 | return true;
|
---|
[743de55] | 73 | } |
---|