EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: crosemffet on December 21, 2017, 06:43:31 AM



Title: validate box for credit card number
Post by: crosemffet on December 21, 2017, 06:43:31 AM
hello everybody, and thanks in advance for your support.
I need to validate one credit card number (luhn algorithm).
I want to use validatebox component.
any help will be appreciated,
regards, and merry christmas..!.


Title: Re: validate box for credit card number
Post by: jarry on December 21, 2017, 05:51:22 PM
You can extend the 'creditcard' validating type and apply it to a inputing box.
Code:
<script type="text/javascript">
$.extend($.fn.validatebox.defaults.rules, {
creditcard: {
validator: function(value,param){
if ( /[^0-9 \-]+/.test( value ) ) {
return false;
}
value = value.replace( /\D/g, "" );
if ( value.length < 13 || value.length > 19 ) {
return false;
}
var nCheck = 0,
nDigit = 0,
bEven = false,
n, cDigit;
for ( n = value.length - 1; n >= 0; n-- ) {
cDigit = value.charAt( n );
nDigit = parseInt( cDigit, 10 );
if ( bEven ) {
if ( ( nDigit *= 2 ) > 9 ) {
nDigit -= 9;
}
}

nCheck += nDigit;
bEven = !bEven;
}
return ( nCheck % 10 ) === 0;
},
message: 'Please enter a valid credit card number.'
}
})
</script>
<input class="easyui-textbox" validType="creditcard">