1. 程式人生 > >銀行卡前臺展示+後臺字串處理+銀行卡卡號驗證

銀行卡前臺展示+後臺字串處理+銀行卡卡號驗證

A。
前臺把字串展示成:四個一堆四個一堆的樣式:

<input class="txt" type="num"  placeholder="確認卡號" 
onkeyup="this.value=this.value.replace(/\D/g,'').replace(/....(?!$)/g,'$& ')"
>

B。後臺資料處理

//第一步:$no----->$str  把分隔開的字串連在一起
$no = '6221 8816 0000 3658 686';
$str =implode(explode(' ',$no));
//第二步:$str----$no   把連在一起的字串分割成隔開的陣列
$str
= '6221881600003658686'; $no =implode(' ',str_split($str,4));

C。銀行卡的php驗證規則—銀行卡校驗規則(Luhn演算法)

//銀行卡格式檢測
function test_bankcode($code)
{
    $arr_no = str_split($code);
    $last_n = $arr_no[count($arr_no) - 1];
    krsort($arr_no);
    $i = 1;
    $total = 0;
    foreach ($arr_no as $n) {
        if ($i % 2
== 0) { $ix = $n * 2; if ($ix >= 10) { $nx = 1 + ($ix % 10); $total += $nx; } else { $total += $ix; } } else { $total += $n; } $i++; } $total -= $last_n; $x
= 10 - ($total % 10); if ($x == $last_n) { return true; } else { return false; } }