Célio A. 4 Denunciar post Postado Janeiro 7, 2012 Gostaria de validar um campo senha sendo que para ser valida a senha tenha que ter no mínimo 6 caracteres, ser alfanumérico e sem caracteres especiais. Como é possível fazer que o php verifique se existe algum caractere alfabético, numérico ou especial? E como contar quantos caracteres tem em uma variável? Compartilhar este post Link para o post Compartilhar em outros sites
Wesley David 20 Denunciar post Postado Janeiro 7, 2012 Colega você pode fazer a primeira verificação via javaScript e para garantir realizar ela novamente no PHP, no PHP utilize expressão regular para aprovar valores alfanuméricos [0-9a-zA-Z] $pattern = '/[0-9a-zA-Z]/'; $result = ""; preg_match($pattern, $subject, $result); print_r($result); /* NÃO TESTEI */ Segue link mas na internet você encontra ótimos matériais (http://profissionais.ws/programacao/php/expressoes-regulares-dissecando-php-02.html) Para definir a quantidade de letras você pode usar strlen(); Abaixo código para usar via javaScript utilize: onkeypress="formata_campo(this,event, 6); para chamar a função e define um maxlength="6" no input para ajudar a bloquear a quantidade de letras digitada function formata_campo(fld, milSep, decSep, e, tamMaximo) { var sep = 0; var key = ''; var i = j = 0; var len = len2 = 0; var strCheck = '0123456789abcdefghijklmnopqrstuvxzwy'; var aux = aux2 = ''; var whichCode = (window.Event) ? e.which : e.keyCode; //if (whichCode == 8 ) return true; //backspace - estamos tratando disso em outra função no keydown if (whichCode == 0 ) return true; if (whichCode == 9 ) return true; //tecla tab if (whichCode == 13) return true; //tecla enter if (whichCode == 16) return true; //shift internet explorer if (whichCode == 17) return true; //control no internet explorer if (whichCode == 27 ) return true; //tecla esc if (whichCode == 34 ) return true; //tecla end if (whichCode == 35 ) return true;//tecla end if (whichCode == 36 ) return true; //tecla home /* O trecho abaixo previne a ação padrão nos navegadores. Não estamos inserindo o caractere normalmente, mas via script */ if (e.preventDefault){ //standart browsers e.preventDefault() }else{ // internet explorer e.returnValue = false } var key = String.fromCharCode(whichCode); // Valor para o código da Chave if (strCheck.indexOf(key) == -1) return false; // Chave inválida if(fld.value.length > tamMaximo) return false; /* Concatenamos ao value o keycode de key, se esse for um número */ fld.value += key; /* Essa parte da função tão somente move o cursor para o final no opera. Atualmente não existe como movê-lo no konqueror. */ if (fld.createTextRange) { var range = fld.createTextRange(); range.collapse(false); range.select(); } else if (fld.setSelectionRange) { fld.focus(); var length = fld.value.length; fld.setSelectionRange(length, length); } return false; } Compartilhar este post Link para o post Compartilhar em outros sites
Célio A. 4 Denunciar post Postado Janeiro 7, 2012 Valeu, vou estudar seguindo suas dicas. Compartilhar este post Link para o post Compartilhar em outros sites