This article mainly introduces a practical case of jquery to verify whether the mailbox format is correct, and uses regular expressions for verification. Interested friends can refer to us to see which mailboxes are available:
It is obviously impossible for us to make judgments by email address.
-A complete Internet email address consists of the following two parts:Login Name @ host name. Domain Name
Separated by the "@" symbol indicating "at" (at) in the middle. The opposite party's login name is on the left of the symbol, and the complete host name is on the right, which consists of the Host Name and domain name. A domain name consists of several parts. Each part is called a Subdomain. each Subdomain is separated by a dot (.). Each Subdomain will tell you information about this email server.
Key validation Regular Expressions:Var myreg =/^ ([\. a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + (\. [a-zA-Z0-9 _-]) + /;
Check input box:
// Verify the email function vailEmail () {var email = jQuery ("# email "). val (); var flag = false; var message = ""; var myreg =/^ ([\. a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + (\. [a-zA-Z0-9 _-]) +/; if (email = '') {message =" Mailbox cannot be blank! ";} Else if (! Myreg. test (email) {message = "enter a valid email address! ";} Else if (checkEmailIsExist () {message =" this email address has been registered! ";}Else {flag = true;} if (! Flag) {// error message // jQuery ("# emailDiv "). removeClass (). addClass ("ui-form-item has-error"); // jQuery ("# emailP" ).html (""); // jQuery ("# emailP" ).html ("<\/I> "+ message); // jQuery (" # email "). focus ();} else {// Correct prompt // jQuery ("# emailDiv "). removeClass (). addClass ("ui-form-item has-success"); // jQuery ("# emailP" ).html (""); // jQuery ("# emailP" ).html ("<\/I> This mailbox is available ");} return flag ;}
Write a method to verify it
// Verify whether the email address has function checkEmailIsExist () {var email = jQuery ("# email "). val (); var flag = false; jQuery. ajax ({url: "checkEmail? T = "+ (new Date ()). getTime (), data: {email: email}, dataType: "json", type: "GET", async: false, success: function (data) {var status = data. status; if (status = "1") {flag = true ;}}); return flag ;}
Background processing program:
@ RequestMapping (value = "/checkEmail", method = RequestMethod. GET) public void checkEmail (HttpServletRequest request, HttpServletResponse response) {Map Map = new HashMap (); Try {String email = request. getParameter ("email"); String status = "0"; // write a query statement to check whether the mailbox exists in the table // UserBaseInfo userBaseInfo = userService. findUserByEmail (email); // if (userBaseInfo! = Null) status = "1"; map. put ("status", status); String data = JSONObject. fromObject (map ). toString (); response. getWriter (). print (data); response. getWriter (). flush (); response. getWriter (). close () ;}catch (Exception ex ){}}
The above is the example code used by jquery to verify whether the email format is correct. The regular expression is used:Var myreg =/^ ([\. a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + (\. [a-zA-Z0-9 _-]) +/, You can try it.