Skip to content

CodeInput Verification Code Input Component ​

This component is generally used in scenarios where the user's SMS verification code needs to be verified, and it can also be used in combination with Hy's keyboard component

📌 Platform Compatibility ​

APP(vue)H5WeChat Mini ProgramAlipay Mini Program
✔✔✔✔

⚠️ Notes ​

Notes

  • When disabledKeyboard is set to true, tapping the input box will not bring up the system keyboard, making it suitable for use with a custom keyboard
  • When the dot parameter is set to true, the input content will be displayed as dots, but event callbacks will return the real value
  • mode supports two modes: box (box mode) and line (bottom line mode)
  • The disabledDot parameter controls whether inputting the decimal point is prohibited; the default is true (input prohibited)

🏯 Basic Usage Example ​

html
<template>
    <hy-code-input v-model="value"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Custom Length ​

html
<template>
    <hy-code-input v-model="value" :maxlength="4"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Box Spacing ​

html
<template>
    <hy-code-input v-model="value" :space="20"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Replace Input Content with Dots ​

html
<template>
    <hy-code-input v-model="value" dot></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Line Mode ​

html
<template>
    <hy-code-input v-model="value" mode="line"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Adjust Colors ​

html
<template>
    <hy-code-input v-model="value" color="#f56c6c" borderColor="#f56c6c"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Hairline Border ​

html
<template>
    <hy-code-input v-model="value" hairline></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Auto Focus ​

html
<template>
    <hy-code-input v-model="value" focus></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Borderless ​

html
<template>
    <hy-code-input v-model="value" :border="false" size="50"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Working with a Custom Keyboard ​

When you need to use a custom keyboard, set disabledKeyboard to true to prevent conflicts between the system keyboard and the custom keyboard. The custom keyboard opens automatically when the input box is tapped and gains focus.

html
<template>
    <view>
        <hy-code-input
            v-model="keyboardValue"
            :disabledKeyboard="true"
            mode="box"
            @finish="handleFinish"
            @focus="showKeyboard = true"
        ></hy-code-input>

        <hy-keyboard
            v-model:show="showKeyboard"
            mode="default"
            v-model="keyboardValue"
            :show-dot-key="false"
            close-text="Done"
            @close="showKeyboard = false"
        ></hy-keyboard>
    </view>
</template>

<script setup>
    import { ref } from 'vue';

    const keyboardValue = ref('');
    const showKeyboard = ref(false);

    const handleFinish = (value) => {
        console.log('Input complete:', value);
        showKeyboard.value = false;
        uni.showToast({
            title: `Input complete: ${value}`,
            icon: 'none',
        });
    };
</script>

Listen to Input Events ​

html
<template>
    <hy-code-input v-model="value" @change="handleChange" @finish="handleFinish"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');

    const handleChange = (value) => {
        console.log('Input changed:', value);
    };

    const handleFinish = (value) => {
        console.log('Input complete:', value);
    };
</script>

Listen to Focus Events ​

html
<template>
    <hy-code-input v-model="value" @focus="handleFocus" @blur="handleBlur"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');

    const handleFocus = () => {
        console.log('Input box focused');
        // You can open a custom keyboard here
    };

    const handleBlur = () => {
        console.log('Input box lost focus');
    };
</script>

API ​

CodeInput Props ​

ParameterDescriptionTypeDefault
v-modelPreset valuestring|number-
adjustPositionWhether the page is automatically pushed up when the keyboard pops upbooleantrue
maxlengthNumber of characters to inputnumber6
borderBorderless input boxbooleantrue
dotWhether to fill with dotsbooleantrue
modeMode selection, see "Basic Usage" above for detailsbox|linebox
hairlineWhether to use a hairline borderbooleanfalse
spaceDistance between charactersnumber10
focusWhether to automatically gain focusbooleanfalse
boldWhether the font and input line are boldbooleanfalse
colorFont colorstring-
fontSizeFont size, unit rpxstring|number18
sizeSize of the input box, width equals height, numeric values default to pxstring|number35
disabledKeyboardDisable bringing up the system keyboard when tapping the input boxbooleanfalse
borderColorBorder and line colorstring-
disabledDotWhether to prohibit inputting the "." symbolbooleantrue
customStyleCustom external styles to applyCSSProperties-
customClassCustom external class namestring-

Events ​

Event NameDescriptionCallback Parameter
changeTriggered when the input content changes, see above for detailsvalue: the current input value
finishTriggered when the number of input characters reaches the maxlength value, see above for detailsvalue: the current input value
focusTriggered when the input box gains focus-
blurTriggered when the input box loses focus-

CSS Variables ​

Variable NameDescriptionDefault Value
--hy-code-input-font-sizeFont size15px
--hy-code-input-dot-sizePassword dot size7px
--hy-code-input-line-widthUnderline width40px
--hy-code-input-text-colorCharacter colorFollows --hy-icon-color
--hy-code-input-dot-radiusPassword dot border radiusFollows --hy-radius-semicircle (100px)
--hy-code-input-no-radiusBorderless mode border radiusFollows --hy-radius-sm (8rpx)
--hy-code-input-cursor-colorCursor colorFollows --hy-text-color
--hy-code-input-line-radiusUnderline border radiusFollows --hy-radius-semicircle (100px)
--hy-code-input-border-colorDefault state border/line colorFollows --hy-border-color--2
12:33
battery