Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to check that user has fill the required fields or not on SignUP button click. But actually I don't know how to do that through JavaScript.

My asp button ID's are username, password, email. These are mandatory fields to fill when user click on SignUp button it should check that these are fill. How can I accomplish that through javascript with asp.net.
Posted
Updated 5-Nov-11 0:05am
v2

C#
<script type="text/javascript">
function Validate()
{
    if(document.getElementById('<%=ddlCountry.ClientID%>').value=="0")
    {
        alert('Please Select Country');
        return false;
    }
   else if(document.getElementById('<%=ddlState.ClientID%>').value=="0")
    {
        alert('Please Select State');
        return false;
    }
   else if(document.getElementById('<%=txtcityName.ClientID%>').value=="")
    {
        alert('Please Enter City Name');
        return false;
    }
        return true;
}
</script>





button should be like this
<asp:button id="imbBtnSubmit" runat="server" onclick="imbBtnSubmit_Click" onclientclick="javascript:return Validate();" xmlns:asp="#unknown" />






you are asking for this script i think plz check this

function Validate()
{

var test="";
    if(document.getElementById('<%=ddlCountry.ClientID%>').value=="0")
    {
        
        test="country,";
        
    }
   if(document.getElementById('<%=ddlState.ClientID%>').value=="0")
    {
    test=test+"state,";
    
    }
   if(document.getElementById('<%=txtcityName.ClientID%>').value=="")
    {
      test=test+"city,";
       
    }
    if(test=="")
    return true;
    else
    {
    if(test.length>0)
    test= test.substr(0,test.length-1);
    alert(test +" field(s) are mandatory please fill those");
    return false;
    }
           
}
 
Share this answer
 
v2
Comments
Ajvirk 5-Nov-11 6:34am    
Thanks for your nice answer. I did this
function show_alert()
{
if(document.getElementById('<%=username.ClientID%>').value==""||document.getElementById('<%=passwrd.ClientID%>').value==""||document.getElementById('<%=emailadd.ClientID%>').value=="")
{
alert("You are missing required fields. Please be carefull to fill the form.");
return false;
}

}
I want one more thing if it is possible that the message should display the filed name which has not fill. Have I to check them 1 by 1 manually or there is a short way.
hitech_s 5-Nov-11 6:48am    
i will try it and will be back in few minutes..........
hitech_s 5-Nov-11 7:03am    
its possible and updated my solution plz check it ..
Ajvirk 5-Nov-11 7:20am    
I can't say that in words what I'm feeling. But only I can say that Thank you very much Sir.
hitech_s 6-Nov-11 23:57pm    
welcome
You can use OnClientClick event as:
ASP.NET
<asp:button id="btn1" runat="server" onclick="btn1_Click" onclientclick="javascript:return fun();" xmlns:asp="#unknown" />


for this write a javascript function:

HTML
function fun1()
{
if(document.getelementbyId(''<%=btn1.ClientId%>').value==0)
alert("Please Enter jkhkjhkj";
return false;
}
 
Share this answer
 
v2
C#
function Validation()
{
var userid=document.getElementById('<%=txtUserID.ClientID %>');
var pwd=document.getElementById('<%=txtPwd.ClientID %>');
var email=document.getElementById('<%=txtemail.ClientID %>');
if(userid.value=="")
{
alert("Please Enter UserID");
userid.focus();
return false;
}
if(pwd.value=="")
{
alert("Please Enter Password");
pwd.focus();
return false;
}
if(email.value=="")
{
alert("Please Enter Password");
email.focus();
return false;
}
}



C#
<asp:button id="btnLogin" runat="server" text="SignUp" onclick="btnLogin_Click" onclientclick="javascript:return Validation();" xmlns:asp="#unknown" />
 
Share this answer
 
v2
You should use the required field validater control of asp.net.
It is very simple. bellow the link i think it will help you.
required field validater control
 
Share this answer
 
v2

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