Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the problem in validation in my project i want to write Validation for each control and message should be displayed below each control in read i want client side java script validation for registration form
plz give me solution....
Posted
Updated 29-Jul-13 23:35pm
v2

Hello Sir,
This is Abhinavsingh993 There are many possible way for the validation among them I am here giving you the javascriptsummary validation method since it is highly tested and a referenced from myone running college project
-------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
Hello Sir,
This is Abhinavsingh993 There are many possible way for the validation among them I am here giving you the javascriptsummary validation method since it is highly tested and a referenced from myone running college project
-------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EmployeeRegistration.aspx.cs" Inherits="Assignment10.EmployeeRegistration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Assignment10 by Abhinav</title>
<link id="Link1" runat="server" rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>

<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 456px;
}
.style3
{
width: 231px;
}
</style>
<script language="javascript"type="text/javascript">
//The Summary Function for the Validations
function validate() {
var summary = "";
summary += isvalidfirstname();
summary += isvalidmiddlename();
summary += isvalidlastname();
summary += isvalidfathername();
summary += isvalidmothername();
summary += isvalidEmail();
summary += isvalidphoneno();
summary += isvalidaltphoneno();
summary += isvalidaltEmail();
summary += isvalidaddress();

if (summary != "") {
alert(summary);
return false;
}
else {

return true;
}
}
///The Validation function of the Mobile Name
function isvalidphoneno() {
var uid;
var temp = document.getElementById("<%=txtmobile.ClientID %>");
uid = temp.value;
var re;
re = /^[0-9]+$/;
var digits = /\d(10)/;
if (uid == "") {
return ("Please enter Primary Mobile Number " + "\n");

}
else if (re.test(uid)) {
return "";
}
else {
return ("Your Mobile Number should be in digits only" + "\n");
}
}
//The Last name Validation Field
function isvalidlastname() {
var uid;
var temp = document.getElementById("<%=txtlastname.ClientID %>");
uid = temp.value;
var re = /^[a-zA-Z ]+$/
if (uid == "") {
return ("Please Enter Your LastName" + "\n");
}
else if (re.test(uid)) {
return "";
}

else {

return ("LastName Field accepts Characters and spaces only" + "\n");

}

}
//The Valid Address Function()
function isvalidaddress() {
var uid;
var temp = document.getElementById("<%=txtaddress.ClientID %>");
uid = temp.value;
var re = /^[a-zA-Z ]+$/
if (uid == "") {
return ("Please Enter Your Address" + "\n");
}
else if (re.test(uid)) {
return "";
}
else {

return ("The Address Field accepts Characters and spaces only" + "\n");
}
}
//The Validation Function for the Middle Name
function isvalidmiddlename() {
var uid;
var temp = document.getElementById("<%=txtmiddlename.ClientID %>");
uid = temp.value;
var re = /^[a-zA-Z ]+$/
if (uid == "") {
return ("Please Enter Your Middle Name" + "\n");
}
else if (re.test(uid)) {
return "";
}
else {

return ("Middle Name Field accepts Characters and spaces only" + "\n");
}

}
//The Validation Function for the Firstname
function isvalidfirstname() {
var uid;
var temp = document.getElementById("<%=txtfirstname.ClientID %>");
uid = temp.value;
var re = /^[a-zA-Z ]+$/
if (uid == "") {
return ("Please Enter Your First Name" + "\n");
}
else if (re.test(uid)) {
return "";
}
else {
return ("First Name Field accepts Characters and spaces only" + "\n");
}
}
//Validation function for the father name
function isvalidfathername() {
var uid;
var temp = document.getElementById("<%=txtfathername.ClientID %>");
uid = temp.value;
var re = /^[a-zA-Z ]+$/
if (uid == "") {
return ("Please Enter Your FatherName" + "\n");
}
else if (re.test(uid)) {
return "";
}

else {
return ("FatherName Field accepts Characters and spaces only" + "\n");

}

}
//The Validation for the Mobile No
function isvalidmothername() {
var uid;
var temp = document.getElementById("<%=txtmothername.ClientID %>");
uid = temp.value;
var re = /^[a-zA-Z ]+$/
if (uid == "") {
return ("Please enter MotherName" + "\n");

}
else if (re.test(uid)) {

return "";
}

else {

return ("MotherName Field accepts Characters and spaces only" + "\n");

}
}
//The validation for the alternate phone no
function isvalidaltphoneno() {
var uid;
var temp = document.getElementById("<%=txtaltmobile.ClientID %>");
uid = temp.value;
var re;
re = /^[0-9]+$/;
var digits = /\d(10)/;
if (uid == "") {
return ("Please Enter Alternate mobile Number" + "\n");
}
else if (re.test(uid)) {
return "";
}

else {

return ("Mobile should be in digits only" + "\n");

}

}
//The Validation function for the Email
function isvalidEmail() {
var uid;
var temp = document.getElementById("<%=txtemail.ClientID %>");
uid = temp.value;
var re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
if (uid == "") {
return ("Please Enter Email" + "\n");
}
else if (re.test(uid)) {
return "";
}
else {

return ("Email should be in the form ex:abc@xyz.com" + "\n");

}
}
//The validaton function for the alternate email
function isvalidaltEmail() {
var uid;
var temp = document.getElementById("<%=txtaltemail.ClientID %>");
uid = temp.value;
var re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
if (uid == "") {
return ("Please Enter Alternate Email" + "\n");

}

else if (re.test(uid)) {
return "";
}
else {

return ("Email should be in the form ex:abc@xyz.com" + "\n");

}
}
</script>
<script language="javascript" type="text/javascript">

function WaterMark(txtfirstname, event) {

var defaultText = "Please Enter Your First Name";

// Condition to check textbox length and event type

if (txtfirstname.value.length == 0 & event.type == "blur") {

//if condition true then setting text color and default text in textbox

txtfirstname.style.color = "Gray";

txtfirstname.value = defaultText;

}

// Condition to check textbox value and event type

if (txtfirstname.value == defaultText & event.type == "focus") {

txtfirstname.style.color = "black";

txtfirstname.value = "";

}

}

</script>
</head>
<body>
<form id="form1" runat="server">




  Employee Registration  
     
Employee First Name <asp:TextBox ID="txtfirstname" runat="server" Height="25px" Width="200px"
MaxLength="20" ToolTip="Please Enter Your First Name" >
<asp:RequiredFieldValidator ID="ReqVal_txtname" runat="server"
ControlToValidate="txtfirstname" ErrorMessage="Mandatory Field">
 
Employee Middle Name <asp:TextBox ID="txtmiddlename" runat="server" Height="25px" Width="200px"
MaxLength="15" ToolTip="Please Enter Your Middle Name">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtmiddlename" ErrorMessage="Mandatory Field">
Employee Last Name <asp:TextBox ID="txtlastname" runat="server" Height="25px" Width="200px"
MaxLength="15" ToolTip="Please Enter Your Last Name">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtlastname" ErrorMessage="Mandatory Field">
Employee FatherName <asp:TextBox ID="txtfathername" runat="server" Height="25px" Width="200px"
MaxLength="30" ToolTip="Please Enter Your Father's Name">
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="txtfathername" ErrorMessage="Mandatory Field">
Employee MotherName <asp:TextBox ID="txtmothername" runat="server" Height="25px"
style="margin-bottom: 0px" Width="200px" MaxLength="30"
ToolTip="Please Enter Your Mother's Name">
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="txtmothername" ErrorMessage="Mandatory Field">
Employee DateofBirth <asp:DropDownList ID="ddlday" runat="server">

<asp:DropDownList ID="ddlmonth" runat="server">

<asp:DropDownList ID="ddlyear" runat="server">

<asp:Label ID="lbl_DateofBirth" runat="server">
Employee Address <asp:TextBox ID="txtaddress" runat="server" Height="91px" TextMode="MultiLine"
Width="193px" MaxLength="100"
ToolTip="Please Enter Your Primary Address for the Correspondence in the Future">
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ControlToValidate="txtaddress" ErrorMessage="Mandatory Field">
Employee AlternateAddress <asp:TextBox ID="txtaltaddress" runat="server" Height="96px"
TextMode="MultiLine" Width="195px" MaxLength="100"
ToolTip="Please Enter Your Alternate Address for the Correspondence">
 
Employee Email <asp:TextBox ID="txtemail" runat="server" Height="25px" Width="200px"
MaxLength="30"
ToolTip="Please Enter Your Primary Email for Correspondence" >
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="txtemail" ErrorMessage="Mandatory Field">
Employee Alternate Email <asp:TextBox ID="txtaltemail" runat="server" Height="25px" Width="200px"
MaxLength="30"
ToolTip="Please Enter Your Alternate Email for the Correspondence">
<asp:Label ID="lbl_success" runat="server">
Employee Mobile No <asp:TextBox ID="txtmobile" runat="server" Height="25px" Width="200px"
MaxLength="12"
ToolTip="Please Enter your Primary Mobile Number for the Contacts in the Future">
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
ControlToValidate="txtmobile" ErrorMessage="Mandatory Field">
Employee Alternate Mobile No <asp:TextBox ID="txtaltmobile" runat="server" Height="25px" Width="200px"
MaxLength="12"
ToolTip="Please Enter Your Alternate Mobile Number for the Contacts in the Future">
 
Employe Pincode <asp:TextBox ID="txtpincode" runat="server" Height="25px" Width="200px"
MaxLength="10" ToolTip="Please Enter Your Pincode for the Communication">
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ControlToValidate="txtpincode" ErrorMessage="Mandatory Field">
Employee Nation <asp:DropDownList ID="ddlnation" runat="server" Height="25px" Width="200px"
ToolTip="Please Select Your Nation">

<asp:Label ID="lbl_Nation" runat="server">
Employee Gender <asp:RadioButtonList ID="rdbgender" runat="server" RepeatDirection="Horizontal"
ToolTip="Please Select Your Gender">
<asp:ListItem Selected="True">Male
<asp:ListItem>Female

 
  <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" OnClientClick ="javascript:validate()"
Text="Register" Height="25px" Width="100px"
ToolTip="On Your Click the Data will be Submitted to the Server and it will be treated as your Final Values" />
 
     
     
  <asp:Label ID="lbl_Status" runat="server">
 



</form>
</body>
</html>
 
Share this answer
 
v3

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