?
Solved

CONVERTING JAVASCRIPT TO ASP

Posted on 2005-04-13
15
Medium Priority
?
255 Views
Last Modified: 2008-03-03

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>
0
Comment
Question by:dict
  • 7
  • 7
15 Comments
 
LVL 13

Expert Comment

by:jrram
ID: 13777566
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
0
 
LVL 11

Expert Comment

by:ajaikumarr
ID: 13778831
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
0
 

Author Comment

by:dict
ID: 13783148
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Expert Comment

by:ajaikumarr
ID: 13787765
Hai,

You are welcome.

Bye
Ajai
0
 

Author Comment

by:dict
ID: 13798722
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
0
 
LVL 11

Expert Comment

by:ajaikumarr
ID: 13799806
Hai,

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

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

Bye
Ajai
0
 
LVL 11

Expert Comment

by:ajaikumarr
ID: 13805211
Hai,

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

Bye
Ajai

0
 

Author Comment

by:dict
ID: 13811338
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
0
 
LVL 11

Accepted Solution

by:
ajaikumarr earned 800 total points
ID: 13812145
Hai,

Check this.

function cpf(form) {
     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;
     }

     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();
}

Bye
Ajai
0
 

Author Comment

by:dict
ID: 13832233
Hi, ajaikumarr,

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


I will add more points to you!

dict
0
 

Author Comment

by:dict
ID: 13832238
More points... were added!

dict
0
 

Author Comment

by:dict
ID: 13832261
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
0
 
LVL 11

Expert Comment

by:ajaikumarr
ID: 13832577
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
0
 

Author Comment

by:dict
ID: 13839120
Exactly!

Many Thanks. The script works fine. very fine!

Best regards

Dict
0
 
LVL 11

Expert Comment

by:ajaikumarr
ID: 13840018
Hai Dict,

Happy Now ... :)

Bye
Ajai
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question