How to check password validation in javascript

Member

makayla.beahan

by makayla.beahan, in category: JavaScript , 6 months ago

This password validation function in Javascript looks really awful to be honest πŸ˜ƒ Thoughts ?


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function checkPassword() {
    const val = document.querySelector('.session-input > #user_password').value
    const confirm = document.querySelector('.session-input > #user_password_confirmation').value
    const email = document.querySelector('.session-input > #user_email').value.split('@')[0]

    if (val && email) {
        if (val.length < 16) {
            document.querySelector('.pw-length').classList.remove('success')
            document.querySelector('.pw-length').classList.add('error')
        } else {
            document.querySelector('.pw-length').classList.remove('error')
            document.querySelector('.pw-length').classList.add('success')
        }

        if (val.search(/[a-z]/) < 0 || val.search(/[A-Z]/) < 0) {
            document.querySelector('.lower-upper').classList.remove('success')
            document.querySelector('.lower-upper').classList.add('error')
        } else {
            document.querySelector('.lower-upper').classList.remove('error')
            document.querySelector('.lower-upper').classList.add('success')
        }

        if (val.search(/[0-9]/) < 0 || val.search(/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/) < 0) {
            document.querySelector('.num-char').classList.remove('success')
            document.querySelector('.num-char').classList.add('error')
        } else {
            document.querySelector('.num-char').classList.remove('error')
            document.querySelector('.num-char').classList.add('success')
        }

        if (val != confirm) {
            document.querySelector('.pw-match').classList.remove('success')
            document.querySelector('.pw-match').classList.add('error')
        } else {
            document.querySelector('.pw-match').classList.remove('error')
            document.querySelector('.pw-match').classList.add('success')
        }

        if (checkIdenticalConsecutive()) {
            document.querySelector('.consecutive').classList.remove('success')
            document.querySelector('.consecutive').classList.add('error')
        } else {
            document.querySelector('.consecutive').classList.remove('error')
            document.querySelector('.consecutive').classList.add('success')
        }

        if (val.search(/hydra/) >= 0 || val.toLowerCase().search(email.toLowerCase()) >= 0) {
            document.querySelector('.name-email-hydra').classList.remove('success')
            document.querySelector('.name-email-hydra').classList.add('error')
        } else {
            document.querySelector('.name-email-hydra').classList.remove('error')
            document.querySelector('.name-email-hydra').classList.add('success')
        }
    }
}
Facebook Twitter LinkedIn Telegram Whatsapp Pocket

no answers