quarta-feira, 30 de janeiro de 2008

capitalizeMe()

Função para capitalizar a primeira letra de cada palavra entrou num campo de formulário.

<
SCRIPT LANGUAGE="JAVASCRIPT">
<!--
function capitalizeMe(obj) {

val = obj.value;

newVal = '';

val = val.split(' ');

for(var c=0; c < val.length; c++) {

newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
}

obj.value = newVal;

}


// -->

</SCRIPT>


Exemplo de uso.:

<FORM NAME="TEST">

<INPUT TYPE="TEXT" NAME="TESTER" onChange="capitalizeMe(this)">
</FORM>

sexta-feira, 25 de janeiro de 2008

ReadFile()

Lê um arquivo no modo texto.
O FileName pode ser um caminho absoluto ou relativo.

Function ReadFile(ByVal FileName)

On Error Resume Next

If Mid(FileName, 2, 1) <> ":" Then
FileName = Server.MapPath(FileName)
End If
If Err Then Err.Clear

On Error GoTo 0

Dim F
Set F = OpenFile(FileName, 1, False)

If F Is Nothing Then
ReadFile = ""
Exit Function
End If

If Not F.AtEndOfStream Then
ReadFile = F.ReadAll
Else
ReadFile = ""
End If

F.Close
Set F = Nothing

End Function

quarta-feira, 16 de janeiro de 2008

ReadDir()

Lê o conteúdo de uma pasta e retorna uma array de arrays.
A array retornada tem essa estrutura:
Arr(...)(0) => Nome do objeto
Arr(...)(1) => Caminho físico completo do objeto
Arr(...)(2) => Tipo de objeto (1 = arquivo, 2 = pasta)
Retorna Null se ocorrer erro.
O DirName pode ser um caminho absoluto ou relativo.

Function ReadDir(ByVal DirName)

On Error Resume Next

If Mid(DirName, 2, 1) <> ":" Then
DirName = Server.MapPath(DirName)
End If
If Err Then Err.Clear

On Error GoTo 0

Dim FSO, D
Dim Arr, i, El, Aux(2)

Arr = Array()
i = -1

Set FSO = Server.CreateObject("Scripting.FileSystemObject")

On Error Resume Next

Set D = FSO.GetFolder(DirName)
If Err Then
Set FSO = Nothing
ReadDir = Null
Exit Function
End If

On Error GoTo 0

For Each El In D.SubFolders
Aux(0) = El.Name
Aux(1) = El.Path
Aux(2) = 2
i = i + 1
ReDim Preserve Arr(i)
Arr(i) = Aux
Next

For Each El In D.Files
Aux(0) = El.Name
Aux(1) = El.Path
Aux(2) = 1
i = i + 1
ReDim Preserve Arr(i)
Arr(i) = Aux
Next

Set D = Nothing
Set FSO = Nothing
ReadDir = Arr

End Function

quarta-feira, 9 de janeiro de 2008

ReadBinary()

Lê um arquivo no modo binário.
O FileName pode ser um caminho absoluto ou relativo.

Function ReadBinary(ByVal FileName)

On Error Resume Next

If Mid(FileName, 2, 1) <> ":" Then
FileName = Server.MapPath(FileName)
End If
If Err Then Err.Clear

On Error GoTo 0

Dim Strm
Set Strm = Server.CreateObject("Adodb.Stream")
Strm.Type = 1 'Binary
Strm.Open

On Error Resume Next

Strm.LoadFromFile FileName

If Err Then
Err.Clear
Set Strm = Nothing
ReadBinary = Null
On Error GoTo 0
Exit Function
End If

On Error GoTo 0

ReadBinary = Strm.Read
Strm.Close
Set Strm = Nothing

End Function

sexta-feira, 4 de janeiro de 2008

ValidaCPF()

Função em C# para validar um número de CPF.

public bool ValidaCPF(string cpf)
{
int[] multiplicador1 = new int[9] { 10, 9, 8, 7, 6, 5, 4, 3, 2 };
int[] multiplicador2 = new int[10] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 };
string tempCpf;
string digito;
int soma;
int resto;

cpf = cpf.Trim();
cpf = cpf.Replace(".", "").Replace("-", "");

if (cpf.Length != 11)
return false;

tempCpf = cpf.Substring(0, 9);
soma = 0;
for(int i=0; i<9; style="font-family:courier new;"> soma += int.Parse(tempCpf[i].ToString()) * multiplicador1[i];

resto = soma % 11;
if (resto < face="courier new"> resto = 0;
else
resto = 11 - resto;

digito = resto.ToString();

tempCpf = tempCpf + digito;

soma = 0;
for(int i=0; i<10; style="font-family:courier new;"> soma += int.Parse(tempCpf[i].ToString()) * multiplicador2[i];

resto = soma % 11;
if (resto < face="courier new"> resto = 0;
else
resto = 11 - resto;

digito = digito + resto.ToString();

return cpf.EndsWith(digito);
}

quinta-feira, 3 de janeiro de 2008

OpenFile()

Abre um arquivo e retorna um objeto TextStream.
O FileName pode ser um caminho absoluto ou relativo.

Function OpenFile(ByVal FileName, ByVal oMode, ByVal Create)

Dim FSO, F
Set FSO = Server.CreateObject("Scripting.FileSystemObject")

On Error Resume Next

If Mid(FileName, 2, 1) <> ":" Then
FileName = Server.MapPath(FileName)
End If
If Err Then Err.Clear

Set F = FSO.OpenTextFile(FileName, oMode, Create)

If Err Then
Err.Clear
On Error GoTo 0
Set FSO = Nothing
Set OpenFile = Nothing
Exit Function
End If

On Error GoTo 0

Set OpenFile = F
Set FSO = Nothing

End Function