vue3点击自动生成密码

                <el-form-item label="密 码">
                    <el-input v-model="editObj.password" type="text" placeholder="请输入密码">
                        <template #append>
                            <el-button type="primary" @click="autoGeneratePwd">点击自动生成</el-button>
                        </template>
                    </el-input>
                </el-form-item>
// 自动生产随机密码
const autoGeneratePwd = () => {
    editObj.value.password = generateRandomPassword()
}
const generateRandomPassword = () => {
    const length = Math.floor(Math.random() * 9) + 8 // 随机生成8-16之间的密码长度
    const lowerCaseChars = 'abcdefghijklmnopqrstuvwxyz'
    const upperCaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    const numberChars = '0123456789'

    let password = ''

    while (password.length < length) {
        const randomCharType = Math.floor(Math.random() * 3) // 随机选择字符类型:0代表小写字母,1代表大写字母,2代表数字

        if (randomCharType === 0) {
            password += lowerCaseChars[Math.floor(Math.random() * lowerCaseChars.length)]
        } else if (randomCharType === 1) {
            password += upperCaseChars[Math.floor(Math.random() * upperCaseChars.length)]
        } else {
            password += numberChars[Math.floor(Math.random() * numberChars.length)]
        }
    }

    return password
}

点击按钮即可生成密码串

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值