jQuery: creare dinamicamente una password per gli utenti
Con jQuery possiamo creare dinamicamente una password per gli utenti.
Con jQuery possiamo creare dinamicamente una password per gli utenti.
Partiamo da questa struttura HTML:
<div class="form-group">
<input type="text" name="password" id="password" class="form-control">
<div class="pt-4">
<a href="#password" class="create-pwd">Genera</a>
</div>
</div>
In jQuery avremo:
function createPassword( minLength ) {
var chars = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split( "" );
return chars.sort(function() { return Math.random() - 0.5; }).join( "" ).substring( 0, minLength );
}
$(function() {
if( $( ".create-pwd" ).length ) {
$( ".create-pwd" ).click(function() {
var pwd = createPassword( 10 );
$( $( this ).attr( "href" ) ).val( pwd ).trigger( "focus" );
return false;
});
}
});
Commenti