Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title lang="PT">Algoritmo do nº do Bilhete de Identidade / PT Citizen ID card algorithm</title>
- <script src="bi.js"></script>
- </head>
- <body>
- <form id="idForm">
- <fieldset>
- <legend>Dados do BI</legend>
- <label for="idBI">Um nº de BI: </label>
- <!-- aqui pretendo impedir a escrita de letras, só aceitar digitos -->
- <input type="text" id="idBI"><br>
- <label for="idDC">Digito de controlo: </label>
- <!-- aqui pretendo impedir a escrita de letras, só aceitar digitos -->
- <!-- só com HTML, não temos solução para essa restrição -->
- <!-- a restrição terá q ser programada com captura de keyboard-related events em JS -->
- <input type="text" id="idDC" size="4">
- </fieldset>
- <input type="submit" value="submeter dados">
- </form>
- <hr>
- <section id="idSectionFeedback"></section>
- </body>
- </html>
- ***
- //bi.js
- window.onload = boot
- const ID_FORM="idForm",
- ID_BI = "idBI",
- ID_DC = "idDC",
- ID_SECTION_FEEDBACK = "idSectionFeedback"
- var oForm, oBI, oDC, oSectionFeedback
- function id(pId){return document.getElementById(pId)}
- function boot(){
- oForm = id(ID_FORM)
- oBI = id(ID_BI)
- oDC = id(ID_DC)
- oSectionFeedback = id(ID_SECTION_FEEDBACK)
- oBI.onkeydown = oDC.onkeydown = actionOnlyDigits
- oBI.oninput = oDC.oninput = actionShowSomaDoBI
- oForm.onsubmit = actionAcceptOrNot
- }//boot
- //event handler
- function actionAcceptOrNot(){
- var dcCorrect = digitoControlo(oBI.value)
- var bAccept = Number(oDC.value)===dcCorrect
- if (bAccept){
- oSectionFeedback.innerHTML = "<mark>OK, não me enganas.</mark>"
- }
- else{
- oSectionFeedback.innerHTML = "<mark>Presta atenção ao digito de controlo.</mark>"
- }
- return false
- }//actionAcceptOrNot
- function actionShowSomaDoBI(e){
- var theEvent = e?e:window.event
- var theTarget = theEvent.target ? theEvent.target : theEvent.srcElement
- var theSum = somaDoBI (oBI.value)
- var theDC = digitoControlo(oBI.value)
- var anterior = oSectionFeedback.innerHTML
- oSectionFeedback.innerHTML = "<br>Soma do BI = "+theSum
- oSectionFeedback.innerHTML += "<br>DC = "+theDC
- //oSectionFeedback.innerHTML += anterior
- return false
- }//actionShowSomaDoBI
- function somaDoBI(pBI){
- var bi = pBI+""
- for(
- var peso=2, pos=bi.length-1, soma=0;
- pos>=0;
- peso+=1, pos-=1
- ){
- var digito = Number(bi[pos])
- var parcela = digito * peso
- soma+=parcela
- }
- return soma
- }//somaDoBI
- function actionOnlyDigits(e){
- var theEvent = e ? e : window.event
- //theTarget é o objeto em q o evento aconteceu
- var theTarget = theEvent.target ?
- theEvent.target : theEvent.srcElement
- var iCode = theEvent.which ?
- theEvent.which : theEvent.keyCode
- //observar os códigos
- //oSectionFeedback.innerHTML = iCode
- var code0 = "0".charCodeAt(0)
- var code9 = "9".charCodeAt(0)
- var bAcceptableCode = iCode>=code0 && iCode<=code9
- var bCodeLeft = iCode===37
- var bCodeRight = iCode===39
- var bCodeDelete = iCode===8
- var bAlwaysAcceptable = bCodeLeft || bCodeRight || bCodeDelete
- var bLetTheEventKeepGoing =
- (bAcceptableCode || bAlwaysAcceptable)?
- true : false
- switch(theTarget.id){
- case ID_BI:
- return bLetTheEventKeepGoing
- break
- case ID_DC:
- if (bAlwaysAcceptable)
- return true
- else
- return bLetTheEventKeepGoing && (oDC.value.length<1)
- break
- }
- return bLetTheEventKeepGoing
- }//actionOnlyDigits
- function digitoControlo(pBI){
- var soma = somaDoBI(pBI)
- for (var dc=0; dc<=10; dc+=1){
- var novaSoma = soma + dc
- var bM11 = novaSoma % 11 === 0
- if (bM11) return dc
- }
- }//digitoControlo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement