Advertisement
am_dot_com

SW 2022-05-20

May 20th, 2022 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title lang="PT">Algoritmo do nº do Bilhete de Identidade / PT Citizen ID card algorithm</title>
  6. <script src="bi.js"></script>
  7. </head>
  8. <body>
  9. <form id="idForm">
  10. <fieldset>
  11. <legend>Dados do BI</legend>
  12. <label for="idBI">Um nº de BI: </label>
  13. <!-- aqui pretendo impedir a escrita de letras, só aceitar digitos -->
  14. <input type="text" id="idBI"><br>
  15. <label for="idDC">Digito de controlo: </label>
  16. <!-- aqui pretendo impedir a escrita de letras, só aceitar digitos -->
  17. <!-- só com HTML, não temos solução para essa restrição -->
  18. <!-- a restrição terá q ser programada com captura de keyboard-related events em JS -->
  19. <input type="text" id="idDC" size="4">
  20. </fieldset>
  21. <input type="submit" value="submeter dados">
  22. </form>
  23. <hr>
  24. <section id="idSectionFeedback"></section>
  25. </body>
  26. </html>
  27.  
  28. ***
  29. //bi.js
  30. window.onload = boot
  31.  
  32. const ID_FORM="idForm",
  33. ID_BI = "idBI",
  34. ID_DC = "idDC",
  35. ID_SECTION_FEEDBACK = "idSectionFeedback"
  36.  
  37. var oForm, oBI, oDC, oSectionFeedback
  38.  
  39. function id(pId){return document.getElementById(pId)}
  40.  
  41. function boot(){
  42. oForm = id(ID_FORM)
  43. oBI = id(ID_BI)
  44. oDC = id(ID_DC)
  45. oSectionFeedback = id(ID_SECTION_FEEDBACK)
  46.  
  47. oBI.onkeydown = oDC.onkeydown = actionOnlyDigits
  48. oBI.oninput = oDC.oninput = actionShowSomaDoBI
  49.  
  50. oForm.onsubmit = actionAcceptOrNot
  51. }//boot
  52.  
  53. //event handler
  54. function actionAcceptOrNot(){
  55. var dcCorrect = digitoControlo(oBI.value)
  56. var bAccept = Number(oDC.value)===dcCorrect
  57. if (bAccept){
  58. oSectionFeedback.innerHTML = "<mark>OK, não me enganas.</mark>"
  59. }
  60. else{
  61. oSectionFeedback.innerHTML = "<mark>Presta atenção ao digito de controlo.</mark>"
  62. }
  63.  
  64. return false
  65. }//actionAcceptOrNot
  66.  
  67. function actionShowSomaDoBI(e){
  68. var theEvent = e?e:window.event
  69. var theTarget = theEvent.target ? theEvent.target : theEvent.srcElement
  70.  
  71. var theSum = somaDoBI (oBI.value)
  72. var theDC = digitoControlo(oBI.value)
  73. var anterior = oSectionFeedback.innerHTML
  74. oSectionFeedback.innerHTML = "<br>Soma do BI = "+theSum
  75. oSectionFeedback.innerHTML += "<br>DC = "+theDC
  76. //oSectionFeedback.innerHTML += anterior
  77.  
  78. return false
  79. }//actionShowSomaDoBI
  80.  
  81. function somaDoBI(pBI){
  82. var bi = pBI+""
  83.  
  84. for(
  85. var peso=2, pos=bi.length-1, soma=0;
  86. pos>=0;
  87. peso+=1, pos-=1
  88. ){
  89. var digito = Number(bi[pos])
  90. var parcela = digito * peso
  91. soma+=parcela
  92. }
  93. return soma
  94. }//somaDoBI
  95.  
  96. function actionOnlyDigits(e){
  97. var theEvent = e ? e : window.event
  98. //theTarget é o objeto em q o evento aconteceu
  99. var theTarget = theEvent.target ?
  100. theEvent.target : theEvent.srcElement
  101.  
  102. var iCode = theEvent.which ?
  103. theEvent.which : theEvent.keyCode
  104.  
  105. //observar os códigos
  106. //oSectionFeedback.innerHTML = iCode
  107. var code0 = "0".charCodeAt(0)
  108. var code9 = "9".charCodeAt(0)
  109. var bAcceptableCode = iCode>=code0 && iCode<=code9
  110. var bCodeLeft = iCode===37
  111. var bCodeRight = iCode===39
  112. var bCodeDelete = iCode===8
  113. var bAlwaysAcceptable = bCodeLeft || bCodeRight || bCodeDelete
  114.  
  115. var bLetTheEventKeepGoing =
  116. (bAcceptableCode || bAlwaysAcceptable)?
  117. true : false
  118.  
  119. switch(theTarget.id){
  120. case ID_BI:
  121. return bLetTheEventKeepGoing
  122. break
  123. case ID_DC:
  124. if (bAlwaysAcceptable)
  125. return true
  126. else
  127. return bLetTheEventKeepGoing && (oDC.value.length<1)
  128. break
  129. }
  130. return bLetTheEventKeepGoing
  131. }//actionOnlyDigits
  132.  
  133. function digitoControlo(pBI){
  134. var soma = somaDoBI(pBI)
  135.  
  136. for (var dc=0; dc<=10; dc+=1){
  137. var novaSoma = soma + dc
  138. var bM11 = novaSoma % 11 === 0
  139. if (bM11) return dc
  140. }
  141. }//digitoControlo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement