Click here to Skip to main content
15,916,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two questions here

can we use asp.net validation and javascript validator simultaneously ?

i have written this email validation using javascript in aspx file and called function on button but button is not firing onClientClick event and not showing this validation,
so is there any solution about how to write this code in cs file?

i dont wants to call function in cs file of button click event like

ScriptManager.RegisterStartupScript(this, GetType(), "", "checkmail();", true);


but how to write this code in cs file

code i have written in aspx file is

C#
function checkmail()
{
    var mail = document.getElementById('txtremail').value;
    var emailExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([com\co\.\in])+$/;
    if (!mail.match(emailExp)) {
        alert("invalid email");
        return false;
    }
    else {return true;}

}


VB
<asp:ImageButton ID="imgbtnjoin" runat="server"
            ImageUrl="~/images/joinnow.jpg" CssClass="joinbtn"
        ValidationGroup="join" onclick="imgbtnjoin_Click" OnClientClick="return checkmail();"/>
Posted

 
Share this answer
 
Comments
vikasvanvi 25-Jan-14 2:58am    
i have did the same but but its not checking email in textbox,and accepts all email.
that's why i need to implement this code in cs file,and this is what i am asking how to write it in cs file not in aspx or js file
ok so i solved it by replacing getElementById with getElementByName

but now another problem occurred,when i click on submitbutton it shows validation as invalid email but data gets submit

C#
function checkEmail()
  {

      var email = document.getElementsByName('txtremail');
      var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
      if (!filter.test(email.value))
      {
          alert('Please provide a valid email address');
          email.focus;
          return false;
      }
      else {
          return true;
      }
  }


so ,now any solution how to stop data from submitting if email is wrong?
 
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