Hello,
I have written a javascript function for email validation . On the click event of the validate button am calling the function however, the function is not called and returns nothing....so please help me out....plz help me out and do let me know where am going wrong...!
here is the function and code behind :

In the source i have written :

Help with Code Tags (Toggle Plain Text)

<script language="javascript">
<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					
	}
</script><script language="javascript">
<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					
	}
</script>

In the code behind i have written :

Help with Code Tags (Toggle Plain Text)

protected void btnvalidate_Click(object sender, EventArgs e)
    {
        txtemail.Attributes.Add("Onclick", "javascript:echeck();");
    } protected void btnvalidate_Click(object sender, EventArgs e)
    {
        txtemail.Attributes.Add("Onclick", "javascript<b></b>:echeck();");
    }

PLZ HELP ME OUT....WHERE AM I GOING WRONG ? FUNCTION DOES NOT GET CALLED ON THE CLICK OF THE VALIDATE BUTTON !

CYA
ROHAN

Recommended Answers

All 5 Replies

my friend...

asp.net provides a validation controls....

you can use your custom control , RegularExpressing control...

it works on client side..

try this things...hope it helps...!

am very new to this asp.net stuff....so i do not know regularexpresn n all....am just trying new stuff on my own n then asking u ppl when i get stuck....so if u could make things simpler for me...i ll appreciate it....keep rockin

cya
Rohan

my friend...

asp.net provides a validation controls....

you can use your custom control , RegularExpressing control...

it works on client side..

try this things...hope it helps...!

put this code in the .aspx page...

rest will be okey....

<form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Enter Email Address"></asp:Label>
        <asp:TextBox ID="TextBoxEmail" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate="TextBoxEmail" Display="Dynamic" 
            ErrorMessage="Please Enter Email Address"></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
            ControlToValidate="TextBoxEmail" Display="Dynamic" 
            ErrorMessage="Please Enter Valid Email Address" 
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
        <br />
        <asp:Button ID="ButtonSubmit" runat="server" Text="Submit" />
    
    </div>
    </form>

Hope it helps;;

There is an "OnClientClick" attribute for your button. That is where you would put a call to a javascript validation function. Your button control in the aspx page would look something like this:

<asp:Button ID="mybutton" runat="server" onClientClick="return echeck(document.getElementById('txt_email').value);" />

This assumes the ClientID of the textbox you want to validate as an email address is "txt_email". Also, not sure if it was just a copy/paste issue, but it looks like you have too many <script> tags and have your js function repeated.

The validation controls dnanetwork mentioned are also worth checking out. However, they do have browser compatability issues and limitations that JS validation doesn't. It is good to have both in your arsenal.

Error first - do not nest script blocks

<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				}
</script>

Error 2nd: echeck() function need an argument.

protected void btnvalidate_Click(object sender, EventArgs e)
  {
   txtemail.Attributes.Add("Onclick", "javascript<b></b>:echeck('some@aa.com');");
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.