JS generates hexadecimal random number (verification code)

Source: Internet
Author: User

Hexadecimal random numbers can also be generated using JavaScript, but somewhat more complex. In JavaScript, you cannot convert decimal numbers directly to hexadecimal, and you need to do the conversion manually. First use the Math.random () function to generate a random number between 0~15, and then use the Math.ceil () function to take the random number to the whole, the next step is to determine the value, if the value is greater than 9, then the number of 10~15 one by one corresponds to a, b ..., until F. After the conversion is complete, the values are added and finally passed to the valcode.php page.

The complete code for generating hexadecimal random numbers using JavaScript is as follows:

Generate random numbers
functionshowval () {num= ' '; for(i=0;i<4;i++) {//Loop output four-bit verification code TMP= Math.ceil ((math.random () * 15) );//Remove a hexadecimal integerif(tmp > 9) {//To determine the random number in sequenceSwitch(TMP) { Case(10)://If the random number equals 10, change to a num+ = ' a '; Break; Case(11): Num+ = ' B '//If the random number equals 11, change to B Break; Case(12): Num+ = ' C ';//If the random number equals 12, change to C Break; Case(13): Num+ = ' d ';//If the random number equals 13, change to D Break; Case(14): Num+ = ' E 'or//if the random number equals 14, change to E Break; Case(15): Num+ = ' F '//If the random number equals 15, change to F Break; } }Else{num+=tmp; } } $(' Chkid '). src= ' valcode.php?num= ' +num;//passes the generated random number to the image generation page $ (' CHKNM '). Value =num;//Save the value of the random number to the hidden field in the page}

Attach image to generate page code (the function implemented is the CAPTCHA)

<?PHPHeader("Content-type:image/png");//Set page encoding    $num=$_get[' Num '];//get the random number of hyperlinks passed    $imagewidth= 60;//defines the width of the canvas    $imageheight= 18;//defines the height of the canvas    $numimage= Imagecreate ($imagewidth,$imageheight);//Create a canvasImagecolorallocate ($numimage, 240,240,240);//set canvas colors are red, green, yellow, respectively     for($i= 0;$i<strlen($num);$i++){//Loop read random number        $x=Mt_rand(1,8) +$imagewidth*$i/4; $y=Mt_rand(1,$imageheight/4); $color=imagecolorallocate ($numimage,Mt_rand(0,150),Mt_rand(0,150),Mt_rand(0,150));//define the color of an imageImagestring ($numimage, 5,$x,$y,$num[$i],$color);//write a random number to the canvas    }     for($i= 0;$i<200;$i++){//A for Loop statement generates a disturbance line          $randcolor=imagecolorallocate ($numimage,Rand(200,255),Rand(200,255),Rand(200,255));//Define ColorImagesetpixel ($numimage,Rand()%70,Rand()%20,$randcolor);//Generate interference Lines} imagepng ($numimage);//Generate ImagesImagedestroy ($numimage);//Freeing Resources?>

JS generates hexadecimal random number (verification code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.