メインコンテンツまでスキップ

フォーム中の銀行カード番号のフォーマットと検証

1. 使用シーン

この例では、適切なフォームページで銀行番号のフォーマットと検証を実現する方法を紹介します。

2.機能を実現する

2.1フォームページの作成

2.2ページ設定

(1) 設定変数

構成図:

(2) 下記のコードをページjsにコピーする

次の一意の表示を修正することに注意してください。

export function onChange({ value }) {
//防止循环调用
if (!this.state.fixed) {
this.setState({
fixed: true,
});
const formatted = value.replace(/D/g, '').replace(/....(?!$)/g, '$& ')
this.$('textField_l9pdvjxn').setValue(formatted)
this.setState({
fixed: false,
});
}
}

export function customValidation(num) {
// 去除空格
num = (num + '').replace(/D+/g, '')
const regExp = /^([1-9])(d{15}|d{18})$/
// 校验非0开始及长度
if (!regExp.test(num)) {
return false
}
num = num.split('').reverse();
let total = 0
for (let i = 0; i < num.length; i++) {
num[i] = parseInt(num[i]);
total += i % 2 ? 2 * num[i] - (num[i] > 4 ? 9 : 0) : num[i];
}
if (total === 0) {
return false;
}
return (total % 10) === 0;
}

(3) イベントバインディング

function customValidation(num) {
return this.customValidation(num);
}

3.効果を実現する

4.オンラインで試してみます

Copyright © 2025钉钉(中国)信息技术有限公司和/或其关联公司浙ICP备18037475号-4