There are 3 files:
Authcode. php ----- php file generated for the verification code
Authcode.html ----- front-end display page
Dealauthcode. php ----- processing page submitted by ajax to the background to determine whether the verification code is correct
*/
?>
The front-end calls the verification code
| The code is as follows: |
Copy code |
|
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.111cn.net/1999/xhtml">
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = utf-8"/>
<Title> verification code ajax verification </title>
<Style type = "text/css tutorial">
* {Font-size: 12px ;}
A {text-decoration: none ;}
A: hover {text-decoration: underline; color: red ;}
</Style>
<Script language = "webpage effects" src = "http://code.jquery.com/jquery-1.4.2.min.webpage effects"> </script>
<Script language = "webpage effect">
$ (Document). ready (function (){
/* ---------------- Cannot see clearly. Change the image -----------*/
$ ("# Chang_authcode_btn"). click (function (){
Var authcode_url = "authcode. php? T = "+ math. random (0, 1 );
$ ("# Authcode_img"). attr ('src', authcode_url );
});
/* -------------- Check the verification code -----------*/
$ ("# Authcode"). bind ({
'Focusin ': function (){
/**
* Get focus
* I removed the img Image. If only src is changed'
* 'In ie, a small image without images will be displayed,
* Here I choose to remove the img element directly.
*/
$ (This). next ('label'). children ('IMG '). remove ();
$ (This). next ('label'). children ('span '). text ('');
},
'Focusout': function (){
/**
* Losing focus
* The main tasks to be done here are as follows:
* (1) first use webpage special effects to verify whether the verification entered by the user is a four-digit valid character, regular match
* (2) if regular expression matching fails (not a valid 4-character), update the verification code image (that is, re-trigger the click event of the "invisible" a tag)
*/
Var input_authcode = $ (this). val ();
Var authcode_regex = new regexp ('^ [a-z0-9] {4}', 'I ');
If (! Authcode_regex.test (input_authcode) {// The error message is returned to the user because it is not a valid 4-bit string.
$ (This). next ('label'). prepend (""); // add an error icon
$ (This). next ('label'). children ('span '). text ('incorrect verification code format! '); // Add the error message
$ ("# Chang_authcode_btn"). trigger ('click'); // refresh the image again
} Else {// ajax server verification is to submit the verification code entered by the user to a verification page on the server for processing!
$. Get ('dealauthcode. Php', {authcode: input_authcode}, function (check_result ){
If (check_result = 'mis _ match') {// Server Verification failed
$ ("# Authcode"). next ('label'). prepend (""); // add the error icon
$ ("# Authcode"). next ('label'). children ('span '). text ('verification code input error! '); // Add the error message
$ ("# Chang_authcode_btn"). trigger ('click'); // refresh the image again
} Else {// server verification passed
$ ("# Authcode"). next ('label'). prepend (""); // add the correct icon
$ ("# Authcode"). next ('label'). children ('span '). text ('verification code entered correctly! '); // Add the correct prompt information
}
});
}
}
});
});
</Script>
</Head>
<Body>
<Div>
<Div> <a id = "chang_authcode_btn" style = "cursor: pointer"> cannot see clearly? Change one! </A> </div>
<Div> verification code: <input id = "authcode" type = "text" size = "20"/> <label> <span class = "error_msg"> </span> </label> </ div>
</Div>
</Body>
</Html> |
Dealauthcode. php ----- processing page submitted by ajax to the background to determine whether the verification code is correct
| The code is as follows: |
Copy code |
<? Php
Session_start ();
$ Authcode = $ _ get ['authcode'];
// $ _ Session ['authcode'] is generated on the authcode page of the verification code.
If (strtoupper ($ authcode )! = $ _ Session ['authcode']) {
Echo 'mis _ match ';
}
?>
|