Link to home
Start Free TrialLog in
Avatar of dict
dict

asked on

CONVERTING JAVASCRIPT TO ASP


Hi,

I am looking for your helpful to convert the Javascript below to ASP script (working with tables).



<html>
<head>
<title>CPF e CGC</title>
<meta name="description" content="CPF e CGC">
<meta name="keywords" content="cpf, cgc, cnpf, cnpj, receita, fazenda">
<meta name="mytopic" content="Computers & Technology:Math & Science:Mathematics">

<script language="JavaScript">



function cpf(form)
{
cpfNum=(form.cpfNum.value);

if(cpfNum.length!=9){alert("You must introduze the 9 digits, inclusive the zeroes at left.");}

cpfDig1=cpfNum.slice(8);   a1=eval(cpfDig1);
cpfDig2=cpfNum.slice(7,8); a2=eval(cpfDig2);
cpfDig3=cpfNum.slice(6,7); a3=eval(cpfDig3);
cpfDig4=cpfNum.slice(5,6); a4=eval(cpfDig4);
cpfDig5=cpfNum.slice(4,5); a5=eval(cpfDig5);
cpfDig6=cpfNum.slice(3,4); a6=eval(cpfDig6);
cpfDig7=cpfNum.slice(2,3); a7=eval(cpfDig7);
cpfDig8=cpfNum.slice(1,2); a8=eval(cpfDig8);
cpfDig9=cpfNum.slice(0,1); a9=eval(cpfDig9);

cpfPriDig=(a1*9+a2*8+a3*7+a4*6+a5*5+a6*4+a7*3+a8*2+a9)%11;
if(cpfPriDig==10){cpfPriDig=0;}
cpfSegDig=(cpfPriDig*9+a1*8+a2*7+a3*6+a4*5+a5*4+a6*3+a7*2+a8)%11;
if(cpfSegDig==10){cpfSegDig=0;}
cpfDV=cpfPriDig*10+cpfSegDig;
if(cpfDV==0){cpfDV="00";}
if(cpfDV>0&&cpfDV<10){cpfDV="0"+cpfDV;}

form.cpfDV.value=cpfDV;
}
</script>

</head>
<body bgcolor=000000>
<!-- following code added by server. PLEASE REMOVE -->
<!-- preceding code added by server. PLEASE REMOVE -->
<table cellpadding=10 cellspacing=10>
<td width=580 bgcolor=ffffcc valign=top>
<font size="-1">
<b>CGC & CPF</b><p>
<p align=justify>
Saiba como se calcula o N&uacute;mero-Controle do <b>CGC</b> e do <b>CPF</b> do <b>Minist&eacute;rio da Fazenda</b>. Adicionalmente, veja o DV de qualquer número (CPF ou CGC), utilizando as rotinas abaixo. Introduza o número do CPF, sempre COM 9 DÍGITOS (exemplo: 001234567) ou o número do CGC sempre COM 12 DÍGITOS (exemplo: 000123450001):
</p>

<form><center><table border=0><tr>
<td align=center><font size="-1">CPF 9 dígitos</font><br>
<input type=text name=cpfNum size=11></td>
<td valign=bottom><input type=button value="Calcular" onClick="cpf(this.form)"></td>
<td align=center><font size="-1">DV</font><br>
<input type=text name=cpfDV size=2></td>
<td valign=bottom><input type=reset value="Reiniciar"></td>
</tr></table></center></form>

</BODY>
</HTML>
Avatar of jrram
jrram
Flag of United States of America image

cpfNum = Request.Form("cpfNum")

If Len(cpfNum) <> 9 Then

 Response.Write "You must introduze the 9 digits, inclusive the zeroes at left."

End If

cpfDig1=Mid(cpfNum, 9,1)  
a1=CInt(cpfDig1)

cpfDig2=Mid(cpfNum, 8,1)
a2=CInt(cpfDig2)

cpfDig3=Mid(cpfNum, 7,1)
a3=CInt(cpfDig3)

cpfDig4=Mid(cpfNum, 6,1)
a4=CInt(cpfDig4)

cpfDig5=Mid(cpfNum, 5,1)
a5=CInt(cpfDig5)

cpfDig6=Mid(cpfNum, 4,1)
a6=CInt(cpfDig6)

cpfDig7=Mid(cpfNum, 3,1)
a7=CInt(cpfDig7)

cpfDig8=Mid(cpfNum, 2,1)
a8=CInt(cpfDig8)

cpfDig9=Mid(cpfNum, 1,1)
a9=CInt(cpfDig9)

cpfPriDig=(a1*9+a2*8+a3*7+a4*6+a5*5+a6*4+a7*3+a8*2+a9)%11

If cpfPriDig = 10 Then
      cpfPriDig=0
End If

cpfSegDig=(cpfPriDig*9+a1*8+a2*7+a3*6+a4*5+a5*4+a6*3+a7*2+a8)%11

If cpfSegDig = 10 Then
      cpfSegDig=0
End If

cpfDV=cpfPriDig*10+cpfSegDig

If cpfDV =0 Then
      cpfDV="00"
End If

If cpfDV > 0 And cpfDV < 10 Then
      cpfDV="0"+cpfDV
End If
Avatar of ajaikumarr
ajaikumarr

Hai,

Try this.

'
'SAVE THIS PAGE AS TEST.ASP
'
<html>
<head>
<title>CPF e CGC</title>
<meta name="description" content="CPF e CGC">
<meta name="keywords" content="cpf, cgc, cnpf, cnpj, receita, fazenda">
<meta name="mytopic" content="Computers & Technology:Math & Science:Mathematics">

<script language="JavaScript">
function cpf(form) {
      cpfNum=(document.frmTest.cpfNum.value);
      if(cpfNum.length!=9){alert("You must introduze the 9 digits, inclusive the zeroes at left.");}

      cpfDig1=cpfNum.slice(8);   a1=eval(cpfDig1);
      cpfDig2=cpfNum.slice(7,8); a2=eval(cpfDig2);
      cpfDig3=cpfNum.slice(6,7); a3=eval(cpfDig3);
      cpfDig4=cpfNum.slice(5,6); a4=eval(cpfDig4);
      cpfDig5=cpfNum.slice(4,5); a5=eval(cpfDig5);
      cpfDig6=cpfNum.slice(3,4); a6=eval(cpfDig6);
      cpfDig7=cpfNum.slice(2,3); a7=eval(cpfDig7);
      cpfDig8=cpfNum.slice(1,2); a8=eval(cpfDig8);
      cpfDig9=cpfNum.slice(0,1); a9=eval(cpfDig9);

      cpfPriDig=(a1*9+a2*8+a3*7+a4*6+a5*5+a6*4+a7*3+a8*2+a9)%11;
      if(cpfPriDig==10){cpfPriDig=0;}
      cpfSegDig=(cpfPriDig*9+a1*8+a2*7+a3*6+a4*5+a5*4+a6*3+a7*2+a8)%11;
      if(cpfSegDig==10){cpfSegDig=0;}
      cpfDV=cpfPriDig*10+cpfSegDig;
      if(cpfDV==0){cpfDV="00";}
      if(cpfDV>0&&cpfDV<10){cpfDV="0"+cpfDV;}

      document.frmTest.cpfDV.value=cpfDV;

      //To resubmit the form for data saving
      document.frmTest.action = "./Test.asp?Act=SAVE"
      document.frmTest.submit();
}
</script>

<%
'
'MAKE SURE TO CHANGE DATABASE, TABLE AND FIELDS NAME APPROPRIATE
'
If Trim(UCase(Request.QueryString("ACT"))) = "SAVE" Then
      Dim svlCPFDVValue
      svlCPFDVValue = Request.Form("cpfDV")

      'code to save the values on database
      Set adoCon = Server.CreateObject("ADODB.Connection")

      'Set an active connection to the Connection object using a DSN-less connection
      adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("myDB.mdb")

      'Create an ADO recordset object
      Set rsmyTable = Server.CreateObject("ADODB.Recordset")

      'Initialise the strSQL variable with an SQL statement to query the database
      strSQL = "SELECT * FROM myTable;"


      'Set the cursor type we are using so we can navigate through the recordset
      rsmyTable.CursorType = 2

      'Set the lock type so that the record is locked by ADO when it is updated
      rsmyTable.LockType = 3

      'Open the rsMyTable table using the SQL query held in the strSQL varaiable
      rsMyTable.Open strSQL, adoCon
      
      'Tell the recordset we are adding a new record to it
      rsMyTable.AddNew

      'Add a new record to the recordset
      rsMyTable.Fields("cpfDV") = svlCPFDVValue

      'Write the updated recordset to the database
      rsMyTable.Update

      'Reset server objects
      rsMyTable.Close
      Set rsMyTable = Nothing
      Set adoCon = Nothing
End If
%>

</head>
<body bgcolor=000000>
<!-- following code added by server. PLEASE REMOVE -->
<!-- preceding code added by server. PLEASE REMOVE -->
<form name="frmTest" id="frmTest" method="post"
<table cellpadding=10 cellspacing=10>
      <td width=580 bgcolor=ffffcc valign=top>
            <font size="-1">
                  <b>CGC & CPF</b><p>
                  <p align=justify>
                        Saiba como se calcula o N&uacute;mero-Controle do <b>CGC</b> e do <b>CPF</b>
                        do <b>Minist&eacute;rio da Fazenda</b>. Adicionalmente, veja o DV de qualquer
                        número (CPF ou CGC), utilizando as rotinas abaixo. Introduza o número do CPF,
                        sempre COM 9 DÍGITOS (exemplo: 001234567) ou o número do CGC sempre COM 12
                        DÍGITOS (exemplo: 000123450001):
                  </p>
                  <center>
                  <table border=0>
                        <tr>
                              <td align=center>
                                    <font size="-1">CPF 9 dígitos</font><br>
                                    <input type=text name=cpfNum size=11>
                              </td>
                              <td valign=bottom>
                                    <input type=button value="Calcular" onClick="cpf(this.form)">
                              </td>
                              <td align=center>
                                    <font size="-1">DV</font><br>
                                    <input type=text name=cpfDV size=2>
                              </td>
                              <td valign=bottom>
                                    <input type=reset value="Reiniciar">
                              </td>
                        </tr>
                  </table>
                  </center>
            </font>
      </td>
</table>
</form>
</BODY>
</HTML>

Bye
Ajai
Avatar of dict

ASKER

Hi, ajaikumarr

Thank you very much.

Do you have interest in to know the goal of this script ?

Well, I think use this script like a password, I need only adapt this script to another to use it like a password.

Thank you again.

dict
Hai,

You are welcome.

Bye
Ajai
Avatar of dict

ASKER

Hi, ajaikumarr

This is a upgrade of that script, but was written in PERL. Here I put a part of this script, which has the goal to consider 'NULL' the sequence of 0,1,2,... until 9.

MY QUESTION
____________

If I increase more points could you help me please, adding these features to your script in ASP? Or I must open a new thread?

<?
$nulo = "12345678909\";
$nulo1 = \"11111111111\";
$nulo2 = \"22222222222\";
$nulo3 = \"33333333333\";
$nulo4 = \"44444444444\";
$nulo5 = \"55555555555\";
$nulo6 = \"66666666666\";
$nulo7 = \"77777777777\";
$nulo8 = \"88888888888\";
$nulo9 = \"99999999999\";
$nulo0 = \"00000000000\";

if (($cpf == $nulo) || ($cpf == $nulo1) || ($cpf == $nulo2)
|| ($cpf == $nulo3) || ($cpf == $nulo4)
|| ($cpf == $nulo5) || ($cpf == $nulo6)
|| ($cpf == $nulo7) || ($cpf == $nulo8)
|| ($cpf == $nulo9) || ($cpf == $nulo0)) {
     echo \"The CPF is \".\"$cpf\".\" wrong\";
}
else {
       
     for ($i=0; $i<11; $i++) {
           $cpf_temp[$i]=\"$cpf[$i]\";
     }

.......


Thank you

dict
Hai,

Sorry Dict, I dont have experience on Perl...

But can understand a bit... Will try it for you.

Bye
Ajai
Hai,

Sorry.... Can u please explain it a bit...

Bye
Ajai

Avatar of dict

ASKER

Hi,

I wish you includes these series of numbers as NULLS in that script you wrote to me. For example:

If cpfNum = "123456789" or cpfNum= "111111111" or cpfNum = "222222222" ...  or cpfNum =  
"00000000000"

{alert("You must introduze the 9 digits in another way;}

Evidently it is not javascript but in this way you can have an idea I want?

Thank you

dict
ASKER CERTIFIED SOLUTION
Avatar of ajaikumarr
ajaikumarr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dict

ASKER

Hi, ajaikumarr,

Thank you very much. I am very grateful by your helpful.


I will add more points to you!

dict
Avatar of dict

ASKER

More points... were added!

dict
Avatar of dict

ASKER

Your reply was accepted!


But... in true... remainder a 'bug'???

I ignoring the problem, because the script is good, but there is a conflict between the two "if" statement! Both not works well!

cpfNum=(document.frmTest.cpfNum.value);
     if(cpfNum.length!=9) {
     alert("You must introduze the 9 digits, inclusive the zeroes at left.");
     return false;
     }

     //ADDED HERE FOR VALIDATION
     if(cpfNum=="123456789" || cpfNum=="111111111" || cpfNum=="222222222" || cpfNum=="333333333" || cpfNum=="444444444" || cpfNum=="555555555" || cpfNum=="666666666" || cpfNum=="777777777" || cpfNum=="888888888" || cpfNum=="999999999" || cpfNum=="000000000") {
     alert("You must introduze the 9 digits in another way;
     return false;
     }


Thank you again

dict
Hai Dict,

Thanks for points... Bit worried about the comments...

Try this.

cpfNum = document.frmTest.cpfNum.value;
if(cpfNum.length < 9) {
      alert("You must introduze the 9 digits, inclusive the zeroes at left.");
      return false;
}else if(cpfNum=="123456789" || cpfNum=="111111111" || cpfNum=="222222222" || cpfNum=="333333333" || cpfNum=="444444444" || cpfNum=="555555555" || cpfNum=="666666666" || cpfNum=="777777777" || cpfNum=="888888888" || cpfNum=="999999999" || cpfNum=="000000000") {
      alert("You must introduze the 9 digits in another way");
      return false;
}

Hope this is what you need :)???
Bye
Ajai
Avatar of dict

ASKER

Exactly!

Many Thanks. The script works fine. very fine!

Best regards

Dict
Hai Dict,

Happy Now ... :)

Bye
Ajai