Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Java C#4.0
Hello Friend,
 
I am working on website in which I have to used both client side and server side validation on Registration Page. But I am unable to do it. Its Only doing server side. Please let me know how can I do both validation.
Thanks in Advance.
 
<script language="javascript" type="text/javascript">
        function validate() {
 
            var Firstname = document.getElementById('<%=txtName.ClientID %>').value;
            var Email = document.getElementById('<%=txtEmail.ClientID %>').value;
            var Address = document.getElementById('<%=txtAddress.ClientID %>').value;
            var City = document.getElementById('<%=txtCity.ClientID %>').value;
            var Zip = document.getElementById('<%=txtZipCode.ClientID %>').value;
            var Contact_No = document.getElementById('<%=txtContactNo %>').value;
            var User_Name = document.getElementById('<%=txtUserName.ClientID %>').value;
            var Password = document.getElementById('<%=txtPassword.ClientID %>').value;
 
            var errormessage = "";
            if (Firstname == "") {
                errormessage += "Enter Your First Name\n";
            }
            if (Email == "") {
                errormessage += "Enter Mail ID\n";
            }
 
            if (Address == "") {
                errormessage += "Enter Address ID\n";
            }
 
            if (City == "") {
                errormessage += "Enter City ID\n";
            }
 
            if (Zip == "") {
                errormessage += "Enter Zip ID\n";
            }
 
            if (Contact_No == "") {
                errormessage += "Enter Contact ID\n";
            }
 
            if (User_Name == "") {
                errormessage += "Enter UserName ID\n";
            }
 
            if (Password == "") {
                errormessage += "Enter Password ID\n";
            }
 
            if (errormessage != "") {
                alert(errormessage);
                return false;
            }
        }
    </script>


 
// Table
 
<div>
        <table align="center" class="style1">
            <tr>
                <td class="style2">
                    &lt;asp:Label ID="Label1" runat="server" Text="Name"&gt;
                </td>
                <td class="style3">
                    &lt;asp:TextBox ID="txtName" runat="server"&gt;
                </td>
                <td class="style4">
                    &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                        ForeColor="Red" ControlToValidate="txtName"&gt;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &lt;asp:Label ID="Label2" runat="server" Text="Email"&gt;
                </td>
                <td class="style3">
                    &lt;asp:TextBox ID="txtEmail" runat="server"&gt;
                </td>
                <td class="style4">
                    &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
                        ForeColor="Red" ControlToValidate="txtEmail"&gt;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &lt;asp:Label ID="Label3" runat="server" Text="Address"&gt;
                </td>
                <td class="style3">
                    &lt;asp:TextBox ID="txtAddress" runat="server"&gt;
                </td>
                <td class="style4">
                    &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
                        ForeColor="Red" ControlToValidate="txtAddress"&gt;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &lt;asp:Label ID="Label4" runat="server" Text="City"&gt;
                </td>
                <td class="style3">
                    &lt;asp:TextBox ID="txtCity" runat="server"&gt;
                </td>
                <td class="style4">
                    &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="*"
                        ForeColor="Red" ControlToValidate="txtCity"&gt;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &lt;asp:Label ID="Label5" runat="server" Text="Zip"&gt;
                </td>
                <td class="style3">
                    &lt;asp:TextBox ID="txtZipCode" runat="server"&gt;
                </td>
                <td class="style4">
                    &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="*"
                        ForeColor="Red" ControlToValidate="txtZipCode"&gt;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &lt;asp:Label ID="Label6" runat="server" Text="Contact No"&gt;
                </td>
                <td class="style3">
                    &lt;asp:TextBox ID="txtContactNo" runat="server"&gt;
                </td>
                <td class="style4">
                    &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="*"
                        ForeColor="Red" ControlToValidate="txtContactNo"&gt;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &lt;asp:Label ID="Label7" runat="server" Text="userName"&gt;
                </td>
                <td class="style3">
                    &lt;asp:TextBox ID="txtUserName" runat="server"&gt;
                </td>
                <td class="style4">
                    &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="*"
                        ForeColor="Red" ControlToValidate="txtUserName"&gt;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &lt;asp:Label ID="Label8" runat="server" Text="Pwd"&gt;
                </td>
                <td class="style3">
                    &lt;asp:TextBox ID="txtPassword" runat="server"&gt;
                </td>
                <td class="style4">
                    &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="*"
                        ForeColor="Red" ControlToValidate="txtPassword"&gt;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;
                </td>
                <td class="style3">
                    &lt;asp:Button ID="btnSubmit" runat="server" Text="Button" OnClientClick="javascript:validate();" /&gt;
                </td>
                <td class="style4">
                    &nbsp;
                </td>
            </tr>
        </table>
    </div>
Posted 25-Feb-13 20:14pm
Edited 25-Feb-13 20:19pm
v2
Comments
Sanjeev Alamuri at 26-Feb-13 2:30am
   
did u checked the values of "Firstname, Email, Address, etc"?. if not, use "debugger" and check the values, whether u are getting correct values or undefined. otherwise use "alert('Firstname');" to check the value of "Firstname" etc.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,Try this
 

OnClientClick="return validate();"
  Permalink  
Comments
Neetesh Agarwal at 26-Feb-13 4:14am
   
Thanks Vinay.........
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Well there are two things wrong with this code that I can see. First, none of your ASP form fields have been closed properly. They either need to have a matching close tag, or have the forward slash attribute at the end of each tag. So:
 
            <asp:label id="Label1" runat="server" text="Name">
 
Should be:
            <asp:label id="Label1" runat="server" text="Name"></asp:label>
            
or:
 
            <asp:label id="Label1" runat="server" text="Name" />
 
 
The same should be done for all of your 'asp:TextBox' and 'asp:RequiredFieldValidator' tags.
 
And secondly, one of your Javascript lines has a typo:
 
	        var Contact_No = document.getElementById('<%=txtContactNo %>').value;
 
Should be:
 
	        var Contact_No = document.getElementById('<%=txtContactNo.ClientID %>').value;
 
 
Otherwise the script fails with an 'Object required' error.
 
After these changes the code compiles and works fine for me.
  Permalink  
v5
Comments
Neetesh Agarwal at 26-Feb-13 4:13am
   
var Contact_No = document.getElementById('<%=txtContactNo.ClientID %>').value; That is correct SOlution thanks NIck,, Thats my fault

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

  Print Answers RSS
0 Maciej Los 245
1 OriginalGriff 210
2 Manfred R. Bihy 190
3 CHill60 180
4 _Amy 155
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,177
2 Maciej Los 3,774
3 Peter Leow 3,478
4 CHill60 2,702


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100