Implementation and technical analysis of the verification code generated by js

Source: Internet
Author: User

Implementation and technical analysis of the verification code generated by js

I will share with you a piece of code for generating and verifying the verification code in js.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Title> JS verification code generation </title>

<Style type = "text/css">

. Code {

Background-image: url(code.jpg );

Font-family: Arial;

Font-style: italic;

Color: Red;

Border: 0;

Padding: 2px 3px;

Letter-spacing: 3px;

Font-weight: bolder;

}

. Unchanged {

Border: 0;

}

</Style>

<Script language = "javascript" type = "text/javascript">

Var code; // define the verification code globally

Function createCode (){

Code = "";

Var codeLength = 6; // The length of the Verification Code

Var checkCode = document. getElementById ("checkCode ");

Var selectChar = new Array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C ', 'D', 'E', 'F', 'G', 'h', 'I', 'J', 'k', 'l', 'M ', 'N', 'O', 'P', 'Q', 'R', 's', 't', 'U', 'V', 'w ', 'X', 'y', 'z'); // All characters of the Verification Code composed of candidates.

For (var I = 0; I <codeLength; I ++ ){

Var charIndex = Math. floor (Math. random () * 36 );

Code + = selectChar [charIndex];

}

// Alert (code );

If (checkCode ){

CheckCode. className = "code ";

CheckCode. value = code;

}

}

Function validate (){

Var inputCode = document. getElementById ("input1"). value;

If (inputCode. length <= 0 ){

Alert ("Enter the verification code! ");

} Else if (inputCode! = Code ){

Alert ("Incorrect verification code! ");

CreateCode (); // refresh the verification code

} Else {

Alert ("^-^ OK ");

}

}

</Script>

</Head>

<Body onload = "createCode ()">

<Form action = "#">

<Input type = "text" id = "input1"/> <input type = "text" onclick = "createCode () "readonly =" readonly "id =" checkCode "class =" unchanged "style =" width: 80px "/> <br/>

<Input id = "Button1" onclick = "validate ();" type = "button" value = "OK"/>

</Form>

</Body>

</Html>

As we all know, JavaScript is a client, is it meaningful to make all the verification on the client? Is the verification code generated from the server safe? Is the verification code generated by the front-end secure?

The verification code is dynamic, but it must be identified by the client and the correct value should be returned for normal verification. This is a process problem. if Javascript is used, it is verified on the client. It is basically the same !!! It is best to generate a server, verify the client, confirm the server, and browse normally. Such a process is foolproof.

Related Article

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.