Last active
August 9, 2018 09:26
-
-
Save jhammann/3590e1eced30d22ec870b2b64df5d62b to your computer and use it in GitHub Desktop.
📲 Generate and fill in IMEI numbers automatically based on the input's id, class or name attribute.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[].forEach.call(document.querySelectorAll('input'), function(el) { | |
el.addEventListener('click', function() { | |
//Check the attributes to see if one of them contains IMEI. | |
var classes = el.getAttribute('class'); | |
var id = el.getAttribute('id'); | |
var name = el.getAttribute('name'); | |
var attrStr = [classes, id, name].join(' ').toLowerCase(); | |
if (attrStr.indexOf('imei') > -1) { | |
el.value = generateImei(); | |
} | |
}); | |
}); | |
// Script from: https://dyrk.org/tools/imei/ | |
function generateImei(){var pos;var str=new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);var sum=0;var final_digit=0;var t=0;var len_offset=0;var len=15;var issuer;var rbi=["01","10","30","33","35","44","45","49","50","51","52","53","54","86","91","98","99"];var arr=rbi[Math.floor(Math.random()*rbi.length)].split("");str[0]=Number(arr[0]);str[1]=Number(arr[1]);pos=2;while(pos<len-1){str[pos++]=Math.floor(Math.random()*10)%10}len_offset=(len+1)%2;for(pos=0;pos<len-1;pos++){if((pos+len_offset)%2){t=str[pos]*2;if(t>9){t-=9}sum+=t}else{sum+=str[pos]}}final_digit=(10-sum%10)%10;str[len-1]=final_digit;t=str.join("");t=t.substr(0,len);return t} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment