Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135

    Codice random si duplica

    Ciao, la seguente funzione genera un codice alfanumerico random:

    codice:
    Function RandomPW(myLength)
    	Const minLength = 6
    	Const maxLength = 20
    	
    	Dim X, Y, strPW
    	
    	If myLength = 0 Then
    		Randomize
    		myLength = Int((maxLength * Rnd) + minLength)
    	End If
    	
    	For X = 1 To myLength
    		Y = Int((3 * Rnd) + 1) '(1) Numeric, (2) Uppercase, (3) Lowercase
    		
    		Select Case Y
    			Case 1
    				Randomize
    				strPW = strPW & CHR(Int((9 * Rnd) + 48))
    			Case 2
    				Randomize
    				strPW = strPW & CHR(Int((25 * Rnd) + 65))
    			Case 3
    				Randomize
    				strPW = strPW & CHR(Int((25 * Rnd) + 97))
    		End Select
    	Next
    	
    	RandomPW = strPW
    End Function
    
    	nr_random = RandomPW(10)
    Succede però che ogni tanto la funzione genera lo stesso codice alfanumerico e mi crea confusione nello script; cosa mi consigliate di fare:

    1) Aumentare il numero dei caratteri generati?
    2) Controllare che il codice non sia già presente nel db e se in caso affermativo generare un secondo codice ?

    Grazie
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

  2. #2
    ovviamente la seconda che hai detto

  3. #3
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135
    Originariamente inviato da optime
    ovviamente la seconda che hai detto
    Grazie, al momento non posso provare il controllo che ho scritto così, è corretto posso ottimizarlo?

    codice:
    nr_random = request.form("nr_random")
    
    Sql = "SELECT * from Trsfrt where nr_random = '"& nr_random &"'"
    Set objRS = Server.CreateObject("ADODB.Recordset")
    objRS.Open SQL, conn, 3, 3
    
    if objRS.eof then
       nr_random = request.form("nr_random")
       
    else
    
    Function RandomPW(myLength)
    	Const minLength = 6
    	Const maxLength = 20
    	
    	Dim X, Y, strPW
    	
    	If myLength = 0 Then
    		Randomize
    		myLength = Int((maxLength * Rnd) + minLength)
    	End If
    	
    	For X = 1 To myLength
    		Y = Int((3 * Rnd) + 1) '(1) Numeric, (2) Uppercase, (3) Lowercase
    		
    		Select Case Y
    			Case 1
    				Randomize
    				strPW = strPW & CHR(Int((9 * Rnd) + 48))
    			Case 2
    				Randomize
    				strPW = strPW & CHR(Int((25 * Rnd) + 65))
    			Case 3
    				Randomize
    				strPW = strPW & CHR(Int((25 * Rnd) + 97))
    		End Select
    	Next
    	
    	RandomPW = strPW
    End Function
    
    strfinal = RandomPW(12)
    
       nr_random = strfinal
       
    end if
    
    objRS.close
    set objRS = nothing
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.