Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote the following code to validate the textbox for email. It's not allowing DOT(.) but I want to allow the DOT(.) only once. Which one should I delete in the following code?

JavaScript
if(document.getElementById(txtemail).value=="")
      {
                 alert(Email id can not be blank)
                document.getElementById(txtemail).focus();
                return false;
      }
     var emailPat = /^(\.*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
     var emailid=document.getElementById(txtemail).value;
     var matchArray = emailid.match(emailPat);
     if (matchArray == null)
    {
               alert("Your email address seems incorrect. Please try again.);
               document.getElementById(txtEmail).focus();
               return false;
    }

 }
Posted
Updated 24-Aug-10 7:57am
v3

Your validation expression is wrong.
Use below one
var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/


Or

See below JavaScript code to validate email id. We mostly use it

<script language="Javascript">


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
</script>


Hope this may help you :)
 
Share this answer
 
v2
Comments
Dalek Dave 25-Aug-10 6:25am    
Excellent Answer
Sandesh M Patil 25-Aug-10 6:44am    
Thanks Dalek, Its an honour to have your comment.
You can alternatively make use of regular expression validator
and then click on the expression property and select the internet email address... also select the control to validate and give the textbox which accepts email id.......

Hope this solves your problem. :) :thumbsup:
 
Share this answer
 
v3
Comments
Sandesh M Patil 25-Aug-10 6:22am    
wat if he want to use javascript insted of using regular expression validator?
singlaNitish 25-Aug-10 8:37am    
sir i am new to .net so i wanted to know does there any security issues or so that i should opt for javascript code.
senguptaamlan 25-Aug-10 9:19am    
if you require the validation at client side (ie at browser) then you have to use javascript. RegularExpression validator actually validates the string at client side(by emitting javascript) if you set True to EnableClientScript property. So writing javascript only make the development process a bit long (cause you need to write the logic, check for syntax check)rather than dragging nd dropping a RequiredFieldValidator and RegularExpressionValidator on the page.
singlaNitish 25-Aug-10 10:48am    
so what i think is if both check on client side which is better also in speed aspect so why not to use regular field validator when it is provided in visual studio ....sorry if u feel i am arguing or so....
Sandesh M Patil 26-Aug-10 5:37am    
See buudy, Requirefield validator is useful when we want to validate at both side i.e. Client and server side. However it makes load on the page means page require time to load but javascript get compiled when page first time load and so javascript makes validation faster and page performance increases
User Regular Expression Validation. That will be more elegant code.

<asp:textbox id="txtEmail" runat="server" maxlength="80" width="255px" xmlns:asp="#unknown"></asp:textbox>

               <asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server" xmlns:asp="#unknown">
                   ControlToValidate="txtEmail" ErrorMessage="Invalid Email"
   ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:regularexpressionvalidator>
 
Share this answer
 
v2
Comments
Christian Graus 25-Aug-10 22:10pm    
You should NEVER do client side validation and not also enforce it on the server.
virang_21 25-Aug-10 23:05pm    
What is your rational behind it ?
Dont Do this type of complex operation.

Just Do.
1. attach regularexpression validator to your text box. which have a inbuilt email validation regex.

Thanks,
Dont forget to mark as a answer

Hiren Solanki
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900