html form

Discussion in 'JavaScript' started by opfabi, Nov 15, 2021.

  1. #1
    I want to make a CNP validator ( https://en.wikipedia.org/wiki/Romanian_identity_card) in javascript through a form that has a button and a text input, but I don't know how to make the html part, i only managed the java code.
    
    function validCNP( p_cnp ) {
    
    var i=0 , year=0 , hashResult=0 , cnp=[] , hashTable=[2,7,9,1,4,6,3,5,8,2,7,9];
    
    if( p_cnp.length !== 13 ) { return false; }
    
    for( i=0 ; i<13 ; i++ ) {
    
    cnp = parseInt( p_cnp.charAt(i) , 10 );
    
    if( isNaN( cnp[I] ) ) { return false; }
    
    if( i < 12 ) { hashResult = hashResult + ( cnp[I] * hashTable[I] ); }[/I][/I][/I]
    
    }
    
    hashResult = hashResult % 11;
    
    if( hashResult === 10 ) { hashResult = 1; }
    
    year = (cnp[1]*10)+cnp[2];
    
    switch( cnp[0] ) {
    
    case 1 : case 2 : { year += 1900; } break;
    
    case 3 : case 4 : { year += 1800; } break;
    
    case 5 : case 6 : { year += 2000; } break;
    
    case 7 : case 8 : case 9 : { year += 2000; if( year > ( parseInt( new Date().getYear() , 10 ) - 14 ) ) { year -= 100; } } break;
    
    default : { return false; }
    
    }
    
    if( year < 1800 || year > 2099 ) { return false; }
    
    return ( cnp[12] === hashResult );
    
    console.log(validCNP(numar));
    }
    
    Code (JavaScript):

    If anyone can help me with the html form, i would appreciate it
     
    Last edited by a moderator: Nov 16, 2021
    opfabi, Nov 15, 2021 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    538
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Hi Opfabi, welcome to the forum...
    Are you looking for following HTML structure:
    
    <!DOCTYPE html>
    <html lang="en">
       <head>
         <meta charset="utf-8">
         <title>test page</title>
       </head>
       <body>
         <h1>test page</h1>
    
         <form id="form-cnptest">
           <fieldset>
             <label>numar = <input type="text" name="numar"></label>
             <button type="submit">test CNP</button>
           </fieldset>
         </form>
         
         <script src="script.js"></script>
       </body>
    </html>
    
    
    HTML:
     
    hdewantara, Nov 17, 2021 IP