I recently registered a member. The username is verified using jquery Ajax. Email, verification code
Function check_email ()
{
VaR check_email = $ ("# reg_mail"). Val ();
VaR Reg =/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + ((/. [a-zA-Z0-9 _-] {2, 3}) {1, 2}) $ /;
Flag = reg. Test (check_email );
If (FLAG)
{
VaR email_value = $ ('# reg_mail'). Val ();
$. Get ("[var. base_url] register. php", {check_name: "email", check_value: email_value, async: false}, function (JSON ){
// Response ('{res_mail'{.html (JSON );
If (JSON = "OK ")
{
Certificate ('{res_mail'{.html ("<font color = 'green' font-size = '12'> <B> this email can be registered! </B> </font> ");
Return true;
} Else {
Certificate ('{res_mail'{.html ("<font color = 'red' font-size = '12'> <B> this email has been registered! </B> </font> ");
Return false;
}
});
} Else {
$ ("# Res_mail" 2.16.html ("<font color = 'red' font-size = '12'> <B> enter the correct email address! </B> </font> ");
Return false;
}
}
It was written in the beginning. However, it is always impossible to obtain the returned status true or false and return an undefined. A post on csdn is classic:
VaR Boolean = false;
$. Get (URL, null, function (data) {// This is not difficult to understand, as long as you first know that the methods in jquery return jquery objects or the objects specified by jquery.
// Ajax methods such as get and post of jquery are Asynchronous interaction by default, so they are returned before the get method is executed completely, in this case, BL is the defined BL = false; therefore, false is always returned; the value should be changed to synchronous.
// If you want to return the correct BL value, you must change the get method. Generally, return values are not processed in Ajax.
// You can use $. Data ("Bl", BL); To save your value, and then use $. Data ("Bl.
If (data. indexof ("true")> = 0 ){
$ ("# Mid" Login .html ("Login successful ");
BL = true;
$. Data ("Bl", BL );
}
Else {
$ ("# Mid" ).html ("Incorrect username or password ");
BL = false;
$. Data ("Bl", BL );
}
});
Return BL;
In this way, the returned status can be obtained. Store values using the data method. And then obtain. Another way is
$. Get (URL, {async: false}, function (data) sets the transmission mode to synchronous transmission.
The final modification function is as follows. Test OK.
Function check_email ()
{
VaR check_email = $ ("# reg_mail"). Val ();
VaR Reg =/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + ((/. [a-zA-Z0-9 _-] {2, 3}) {1, 2}) $ /;
Flag = reg. Test (check_email );
If (FLAG)
{
VaR email_value = $ ('# reg_mail'). Val ();
$. Get ("[var. base_url] register. php", {check_name: "email", check_value: email_value, async: false}, function (JSON ){
// Response ('{res_mail'{.html (JSON );
If (JSON = "OK ")
{
Certificate ('{res_mail'{.html ("<font color = 'green' font-size = '12'> <B> this email can be registered! </B> </font> ");
Tamp_email = true;
$. Data ("tamp_email", tamp_email );
// $ ('# Sub_reg'). ATTR ("disabled", false );
} Else {
Certificate ('{res_mail'{.html ("<font color = 'red' font-size = '12'> <B> this email has been registered! </B> </font> ");
Tamp_email = false;
$. Data ("tamp_email", tamp_email );
// $ ('# Sub_reg'). ATTR ("disabled", true );
}
});
Return tamp_email;
} Else {
$ ("# Res_mail" 2.16.html ("<font color = 'red' font-size = '12'> <B> enter the correct email address! </B> </font> ");
Return false;
}
}