PF2G Posted August 22, 2012 at 11:17 AM Report Share #473129 Posted August 22, 2012 at 11:17 AM Boas, eu estou a tentar definir um maxlength de uma input text dependendo da radio button seleccionada: <td> <input type="radio" name="bi_cc" value="BI"> B.I.</input> <input type="radio" name="bi_cc" value="CC"> C.C. </input> <input type="radio" name="bi_cc" value="Passaporte"> Passaporte </input> <br/> <br/> <input class="input" type="text" name="bi_cc" value="<?php echo $user_info['bi_cc'] ?>" maxlength=" <?php if (isset($_POST['bi_cc'])) { if ($_POST['bi_cc'] == 'BI') { echo '9'; } else if ($_POST['bi_cc'] == 'CC') { echo '10'; } else if ($_POST['bi_cc'] == 'Passaporte') { echo '11'; } } ?>"/> </td> Mas não esta a acontecer nada. A unica solução que encontrei na minha pesquisa é ao fazer submit mas eu nao quero fazer submit quero que leia atraves do select da radio button. Link to comment Share on other sites More sharing options...
pikax Posted August 22, 2012 at 11:21 AM Report Share #473130 Posted August 22, 2012 at 11:21 AM PHP e' uma linguagem Server-Side, para o que tu queres tens que utilizar Javascript que ' uma Linguagem Client-Side Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender. A beleza de um código está em decompor problemas complexos em pequenos blocos simples. "learn how to do it manually first, then use the wizzy tool to save time." "Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast." Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted August 22, 2012 at 11:23 AM Report Share #473131 Posted August 22, 2012 at 11:23 AM para poderes fazer essa alteração terás de usar javascript. a ideia será registar uma função à alteração do valor dos "radio buttons" que alterará o valor de "maxlength" do teu "input" IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted August 22, 2012 at 11:53 AM Author Report Share #473134 Posted August 22, 2012 at 11:53 AM Será que assim resultará? <script type="text/javascript"> function maxlength(){ if(radio_bi_cc.value=="BI"){ input_bi_cc.maxlength=="9"; } else if(radio_bi_cc.value=="CC"){ input_bi_cc.maxlength=="10"; } else if(radio_bi_cc.value=="Passaporte"){ input_bi_cc.maxlength=="11"; } } </script> E depois como chamo a função na input text onde quero aplicar os diferentes maxlength? Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted August 22, 2012 at 02:20 PM Report Share #473149 Posted August 22, 2012 at 02:20 PM a ideia seria essa, no entanto tens vários problemas com o código que apresentas - radio_bi_cc não é uma variável existente - input_bi_ccnão é uma variável existente - o nome do grupo e o nome da caixa de entrada é igual : "bi_cc" - essa não é a forma de alterar o valor de um atributo de um elemento DOM, tens de usar a função "setAttribute" IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted August 22, 2012 at 02:53 PM Author Report Share #473155 Posted August 22, 2012 at 02:53 PM Podes-me ajudar, sff? Eu tenho pesquisado mas não encontro nada de jeito :S radio_bi_cc e input_bi_cc sao os nomes dos radio button e do input text Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted August 22, 2012 at 03:12 PM Report Share #473158 Posted August 22, 2012 at 03:12 PM (edited) isto está mais ou menos adaptado ao teu exemplo ... <!DOCTYPE html> <html> <head> <script type="text/javascript"> function maxlength(){ var radios = document.getElementsByName("bi_cc"); var val = 0; for( i = 0; i < radios.length; i++ ) { if( radios[i].checked == true ) val = radios[i].value; } if(val == "BI"){ document.getElementsByName("input")[0].setAttribute("maxlength", "9"); } else if(val=="CC"){ document.getElementsByName("input")[0].setAttribute("maxlength", "10"); } else if(val=="Passaporte"){ document.getElementsByName("input")[0].setAttribute("maxlength", "11"); } } </script> </head> <body> <input type="radio" name="bi_cc" value="BI" onchange="maxlength();"> B.I.</input> <input type="radio" name="bi_cc" value="CC" onchange="maxlength();"> C.C. </input> <input type="radio" name="bi_cc" value="Passaporte" onchange="maxlength();"> Passaporte </input> <br/> <br/> <input class="input" type="text" name="input" value="" maxlength="0" /> </body> </html> Edited August 22, 2012 at 03:13 PM by HappyHippyHippo IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted August 22, 2012 at 04:21 PM Author Report Share #473166 Posted August 22, 2012 at 04:21 PM Está fixe Hippo, obrigado. Só mais uma coisa: eu quando escolho o CC digito os 10 numeros e depois se escolher o BI ele continua com os 12, será que ha maneira de apagar de acordo com o limite? Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted August 22, 2012 at 04:24 PM Report Share #473167 Posted August 22, 2012 at 04:24 PM http://www.w3schools.com/jsref/jsref_substring.asp IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted August 23, 2012 at 09:42 AM Author Report Share #473223 Posted August 23, 2012 at 09:42 AM http://www.w3schools.com/jsref/jsref_substring.asp Hippo estou com algumas dificuldades em preceber é algo assim deste genero? document.getElementsByName("input")[0].write(8,12); Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted August 23, 2012 at 10:02 AM Report Share #473225 Posted August 23, 2012 at 10:02 AM Hippo estou com algumas dificuldades em preceber é algo assim deste genero? document.getElementsByName("input")[0].write(8,12); ??? var element = document.getElementsByName("input")[0]; /* guardar o elemento a trabalhar */ element.setAttribute("maxlength", "9"); /* definir o tamanho máximo */ element.value( /* guardar a string no campo value do elemento */ element.value().substring( /* "cortar" a string que existe no campo value do elemento */ 0, /* o resultado do "corte" começa na posição 0 (início) da string */ 9)); /* o resultado do "corte" termina na posição do novo máximo da campo */ IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted August 23, 2012 at 10:18 AM Author Report Share #473227 Posted August 23, 2012 at 10:18 AM E é suposto apagar automaticamente? É que não está a resultar: <script type="text/javascript"> function maxlength(){ var radios = document.getElementsByName("bi_cc"); var element = document.getElementsByName("input")[0]; var val = 0; for( i = 0; i < radios.length; i++ ) { if( radios[i].checked == true ) val = radios[i].value; } if(val == "BI"){ element.setAttribute("maxlength", "8"); element.value(element.value().substring(8, 12)); } else if(val=="CC"){ element.setAttribute("maxlength", "14"); } else if(val=="Passaporte"){ element.setAttribute("maxlength", "8"); element.value(element.value().substring(8, 12)); } } </script> Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted August 23, 2012 at 10:44 AM Report Share #473229 Posted August 23, 2012 at 10:44 AM element.value( /* guardar a string no campo value do elemento */ element.value().substring( /* "cortar" a string que existe no campo value do elemento */ 0, /* o resultado do "corte" começa na posição 0 (início) da string */ 9)); /* o resultado do "corte" termina na posição do novo máximo da campo */ element.value(element.value().substring(8, 12)); não vou dizer nada porque se disser não serão palavras simpáticas ... IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted August 23, 2012 at 10:50 AM Author Report Share #473231 Posted August 23, 2012 at 10:50 AM Eu ponho os 12 digitos do Cartao do Cidadao e quando mudo para BI, nao apaga continuam os 12 digitos. :S Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted August 23, 2012 at 10:56 AM Report Share #473232 Posted August 23, 2012 at 10:56 AM function maxlength(){ var radios = document.getElementsByName("bi_cc"); var input = document.getElementsByName("input")[0]; var val = 0; for( i = 0; i < radios.length; i++ ) { if( radios[i].checked == true ) val = radios[i].value; } var max = 0; if(val == "BI") max = 9; else if(val=="CC") max = 10; else if(val=="Passaporte") max = 11; input.setAttribute("maxlength", max); input.value = input.value.substring(0, max); } IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted August 23, 2012 at 11:13 AM Author Report Share #473234 Posted August 23, 2012 at 11:13 AM Ta fixe, obrigadao. Só mais uma pergunta: eu quero que o BI seja so numeros. Já tenho o script como chamo agora a function? <SCRIPT language=Javascript> <!-- function isNumberKey(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } //--> </SCRIPT> if(val == "BI") { return isNumberKey(evt);/* nao sei se é assim */ max = 8; } Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted August 23, 2012 at 11:18 AM Report Share #473235 Posted August 23, 2012 at 11:18 AM essa função é para ser usada no evento de carregar numa tecla <input onkeydown="isNumberKey()" /> <input onkeypress="isNumberKey()" /> <input onkeyup="isNumberKey()" /> IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
PF2G Posted August 23, 2012 at 11:22 AM Author Report Share #473236 Posted August 23, 2012 at 11:22 AM E não da para usar só quando a radio button for BI Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted August 23, 2012 at 11:24 AM Report Share #473237 Posted August 23, 2012 at 11:24 AM tens de alterar a função para verificar o estado do radio button IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now