Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Password Validation Implementation for Registration Forms

Tech May 10 4
  1. Password Validation Rule

The password must include upperacse letters, lowercase letters, numbers, and special characters, with a length of 8 to 20 chraacters.

var validatePassword = (rule, value, callback) => {
    let pattern = 
        /^(?=.*[A-Za-z])(?=.*\d)(?=.*[`~!@#$%^&*()_+<>?:"{},.\/\\;'[\]])[A-Za-z\d`~!@#$%^&*()_+<>?:"{},.\/\\;'[\]]{8,20}$/;
    if (!value) {
        callback(new Error('Password cannot be empty'));
    } else if (!pattern.test(value)) {
        callback(new Error('Password must contain uppercase, lowercase, numbers, and special characters, length 8-20'));
    } else {
        callback();
    }
};
  1. Confirm Password Validation Rule

Both password fields must match exactly.

var validateConfirmPassword = (rule, value, callback) => {
    var pattern = 
      /^(?=.*[A-Za-z])(?=.*\d)(?=.*[`~!@#$%^&*()_+<>?:"{},.\/\\;'[\]])[A-Za-z\d`~!@#$%^&*()_+<>?:"{},.\/\\;'[\]]{8,20}$/;
    if (!value) {
        callback(new Error('Please confirm your password'));
    } else if (value !== this.formData.password) {
        callback(new Error('Passwords do not match'));
    } else if (!pattern.test(value)) {
        callback(new Error('Password must contain uppercase, lowercase, numbers, and special characters, length 8-20'));
    } else {
        callback();
    }
};
  1. HTML Form Template

<el-form-item prop="password">
    <i class="required">*</i>
    <el-input :show-password="true" type="password" v-model="formData.password" placeholder="Enter password" auto-complete="new-password">
    </el-input>
</el-form-item>
<el-form-item prop="confirmPassword">
    <i class="required">*</i>
    <el-input :show-password="true" type="password" v-model="formData.confirmPassword" placeholder="Confirm password" auto-complete="new-password">
    </el-input>
</el-form-item>
  1. Validation Rules Configuration

rules: {    
    password: [{
        required: true,
        validator: validatePassword,
    }],
    confirmPassword: [{
        required: true,
        validator: validateConfirmPassword,
    }],
},

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.