How to Use jquery to verify whether the email format is correct _ jquery-js tutorial

Source: Internet
Author: User
Tags valid email address subdomain
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.

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.