The QQ website has a website report function. After reading some js code, I thought it was well written and I won it. Below is an email verification and url verification JavaScript code, share it with you for email address verification
The Code is as follows:
Function checkEmail (){
Var email = $. trim ($ ("# report_email"). val ());
If (email = ""){
$ ("# Report_email" pai.next('p'0000.html ("* enter the email address! ");
Return false;
}
Var re =/^ ([a-zA-Z0-9] + [_ |.]?) * [A-zA-Z0-9] + @ ([a-zA-Z0-9] + [_ |.]?) * [A-zA-Z0-9] +. [a-zA-Z] {2, 3} $/ig;
If (! Re. test (email )){
$ ("# Report_email" 2.16.next('p'hangzhou.html ("* Incorrect email address format! ");
Return false;
}
Return true;
}
Url address verification
The Code is as follows:
Function getUrlArray (e, type ){
Var urls = $. trim ($ ("# report_url_list"). val ());
Urls = urls. replace (/s * n + s */ig, "n ");
$ ("# Report_url_list"). val (urls );
// Format the urls
If (type = "blur "){
ReportUrlElm. val (urls );
}
If (e & e. keyCode = 13 ){
ReportUrlElm. val (urls + "n ");
}
Urls = urls. split ("n ");
User_urls = urls;
Return urls;
}
Function checkurls (e, type ){
$ ("# ErrorList"). find ("span"). hide ();
Urls = getUrlArray (e, type );
Var urlfag = true;
Url_index = 0;
Var urlText = "Enter the website to report. You can report multiple websites, with one URL per line and no more than 20 URLs at a time. ";
If (! Urls [0] | urls [0] = urlText ){
$ ("# Report_url_list" ).val(urlText).css ("color", "#757575 ");
$ ("# Report_url_list" pai.next('p'{.html ('* enter the correct URL ');
$. Urlfag = false;
Return false;
}
If (urls. length> 20 ){
$ ("# Report_url_list" pai.next('p'{.html ("more than 20, please report in batches ")
$. Urlfag = false;
Return false;
}
$. Urlfag = true;
For (url_index = 0; url_index $. Urlfag = checkEachUrl (urls, url_index) & $. urlfag;
}
Return urlfag;
}
Function checkEachUrl (urls, index ){
Var e = urls [index];
A = e. replace (/^ http :///,"");
If (a. indexOf ("/")! =-1 ){
A = a. substring (0, a. indexOf ("/"));
}
Var rg =/^ (https? | Ftp | news )://)? ([W-] +.) + (.) + (: d + )? (/[W -./? % & =] *)? $/I;
If (! Rg. test ()){
UrlError ("format error", index,-1 );
Return false;
}
If (inUrlArray (urls, index )){
UrlError ("do not report the same website", index,-1 );
Return false;
}
$. Ajax ({
Async: false,
Url: "http://api.pc.qq.com/tapi/url_report.php? Url = http: // "+,
DataType: "jsonp ",
Method: "GET ",
Success: function (){
If (a. is_existed = "yes "){
UrlError ("this URL has been reported", index,-1 );
$. Urlfag = false;
}
}
});
UrlError ("normal", index, 0 );
Return true;
}
Function inUrlArray (urls, index ){
Var r = false
For (I = index + 1; I A = urls [index]. replace (/^ http (s ?) :///,"");
B = urls [I]. replace (/^ http (s ?) :///,"");
If (a = B)
R = true;
}
Return r;
}
Function urlError (msg, index, type ){
Type = type | 0;
Var test = $ ("# errorList p"). find ("span"). eq (index );
Test. show ();
If (type <0 ){
Test.attr('title', msg316.css ("background-position", "-20px 0px ");
} Else {
Test.attr('title', msg316.css ("background-position", "0px 0px ");
}
}