admin 管理员组

文章数量: 1086019

How can i validate a mail id is a gmail id using javascript/jquery/regex.

For eaxample :

I give [email protected].I need to check this mail id is a gmail id or not? How can i do this?

How can i validate a mail id is a gmail id using javascript/jquery/regex.

For eaxample :

I give [email protected].I need to check this mail id is a gmail id or not? How can i do this?

Share Improve this question asked Feb 13, 2012 at 6:49 Mas UserMas User 1662 gold badges5 silver badges17 bronze badges 3
  • Helpful stackoverflow./questions/6205519/…, stackoverflow./questions/8218345/… – elclanrs Commented Feb 13, 2012 at 6:51
  • 1 What do you mean by gmail? My email is [email protected] and it is a gmail account – lvil Commented Feb 13, 2012 at 7:23
  • Remember to allow the plus sign in the id. I hate it so much when websites tell me that the address I provided is invalid. – curial Commented Feb 13, 2012 at 10:13
Add a ment  | 

4 Answers 4

Reset to default 4
if (/@gmail\.$/.test(string)) {
    // This is a gmail id.
}

Use this regular expression

[a-zA-Z0-9]+\@gmail.

try this

if (/^([A-Za-z0-9_\-\.])+\@([gmail|GMAIL])+\.()$/.test(yourstring)) {
    // Valid  gmail id.
}

USE THE ASCII CODE FOR VALIDATION WITHOUT REGEX

<!DOCTYPE html>
<html>

<head>
 <title>VALIDATION GMAIL ID ONLY</title>  
  <meta charset="utf-8">

<style>

    body{
        margin:0px;
        padding:0px;
    }
    #box{
        background-color:aquamarine;
        height:300px;
        width:700px;
        margin:auto;
    }
    #box label{
        font-size:30px;
        margin-top:80px;
        float:left;
        font-family:arial;
        color:#2b3b08;
        font-weight: bold;
        margin-left:40px;
    }

    #box input{
        margin-top:72px;
        width:350px;
        height:40px;
        border:2px solid black;
        margin-left:20px;
    }

    #box p{
        margin-left:150px;
        font-size:19px;
    }


    #btn1{
        border:none;
        background-color:orange;
        color:white;
        padding:10px;
        width:200px;
        height:40px;
        font-size:20px;
    }

    #btn1:hover{
        cursor:pointer;
    }

    b{
        font-size:20px;
        color:red;
        display:none;
    }

   </style>






</head>

<body>

<div id="box">

<label for="email">Email :</label>

<input type="email" name="email1" id="email" placeholder=" Enter Email            Address " title="PLEASE ENTER YOUR OWN EMAIL">
<br clear="all">
    <p>* Please Enter Only Gmail-email-address</p>
    <center><button type="submit" id="btn1">Submit</button></center>
   <br>
    <center><b id="demo"></b></center>


</div>

<script>

    var  btn1=document.getElementById("btn1");
    btn1.addEventListener('click',verify1);

function verify1()
    {
var email=document.getElementById("email");
var email_value=document.getElementById("email").value;
var email_length=email_value.length;


 var atindex=email_value.indexOf('@');
var dotindex=email_value.indexOf('.');
var mindex=email_value.lastIndexOf('m');




    if(email_length=="")
        {
            document.getElementById("demo").style.display="block";
            document.getElementById("demo").innerHTML="EMAIL ADDRESS IS REQUIRED";
        }
    else
        {
            if(atindex<5||dotindex-atindex!=6||mindex-dotindex!=3)
                {
                    document.getElementById("demo").style.display="block";
            document.getElementById("demo").innerHTML="PLEASE ENTER Valid Email";
                }
            else{
                if(email_value.charCodeAt(atindex+1)!=71&&email_value.charCodeAt(atindex+1)!=103||email_value.charCodeAt(atindex+2)!=109&&email_value.charCodeAt(atindex+2)!=77||email_value.charCodeAt(atindex+3)!=65&&email_value.charCodeAt(atindex+3)!=97||email_value.charCodeAt(atindex+4)!=105&&email_value.charCodeAt(atindex+4)!=73||email_value.charCodeAt(atindex+5)!=76&&email_value.charCodeAt(atindex+5)!=108)
                    {
                         document.getElementById("demo").style.display="block";
            document.getElementById("demo").innerHTML="PLEASE ENTER ONLY GMAIL-ID";
                    }
                else
                    {
                                `document.getElementById("demo").style.display="none";`
                    }   
            }
    }   
    }

</script>







</body>

本文标签: Validating Gmail id using javascript And JqueryStack Overflow