Skip to content

Instantly share code, notes, and snippets.

@muhfaris
Last active July 27, 2017 01:35
Show Gist options
  • Save muhfaris/d057ef765bd36b5fedf0ab519afed2ce to your computer and use it in GitHub Desktop.
Save muhfaris/d057ef765bd36b5fedf0ab519afed2ce to your computer and use it in GitHub Desktop.
Batasi input field ketika di ketikkan
function textLength(text,value){
var maxLength;
if(text == 'cc'){
maxLength = 16;
}else if(text == 'ccc') {
maxLength = 4;
}
if(value.length > maxLength) return false;
return true;
}
$('#id-name-first').keyup(function() {
this.value = this.value.replace(/[^1-9\.]/g,''); //numeric only
var valueOld='';
if(!textLength('cc',this.value)) {
this.value = oldValue;
}else{
oldValue = this.value;
}
console.log($(this).val());
});
$('#id-name-second').keyup(function() {
this.value = this.value.replace(/[^1-9\.]/g,'');
var valueOld='';
if(!textLength('ccc',this.value)) {
this.value = oldValue;
}else{
oldValue = this.value;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment