chakri27
asked on
Client vallidation for CustomValidator control
Hi,
I am developing an application in that three text boxes for first name ,middle name,last name.
if user enter first name and middle name no need of validating last name.
if user enter last name no need of validating first name and middle name.
for this i used custom validator control working perfect but its validating on server side
i want client side also . i want client side and server side at the same time.
can u please provide me clear idea and code that would be appreciated.
servide side Code:working fine
Protected Sub CustomValidator2_ServerVal idate(ByVa l source As Object, ByVal args As System.Web.UI.WebControls. ServerVali dateEventA rgs)
If txtfname.Text.Length <> 0 And txtmidname.Text.Length <> 0 And txtlname.Text.Length = 0 Then
args.IsValid = True
Exit Sub
End If
If txtfname.Text = "" And txtmidname.Text = "" And txtlname.Text <> "" Then
args.IsValid = True
Exit Sub
End If
If txtfname.Text = "" And txtmidname.Text = "" And txtlname.Text = "" Then
args.IsValid = False
'CustomValidator2.ErrorMes sage = " * Please fill First Name and Last Name Or Alias Name"
Exit Sub
End If
End Sub
Client script:
function validatingnames(source, args)
{
var firstname = document.getElementById('< %=txtfname .ClientID% >');
var midname= document.getElementById('< %=txtmidna me.ClientI D%>');
var lname= document.getElementById('< %=txtlname .ClientID% >');
if (firstname.value.length != 0 && midname.value.length != 0 && lname.value.length == 0) {
args.IsValid = true;
return;
}
if(firstname.innerText =="" && midname.innerText =="" && lname.innerText !="") {
args.IsValid = true;
return;
}
if(firstname.innerText == "" && midname.innerText == "" && lname.innerText == "")
{
args.IsValid = false;
return;
}
}
in client validation it validating three fields correct but error message not displaying dynamic even i set display property Display="Dynamic".
I am developing an application in that three text boxes for first name ,middle name,last name.
if user enter first name and middle name no need of validating last name.
if user enter last name no need of validating first name and middle name.
for this i used custom validator control working perfect but its validating on server side
i want client side also . i want client side and server side at the same time.
can u please provide me clear idea and code that would be appreciated.
servide side Code:working fine
Protected Sub CustomValidator2_ServerVal
If txtfname.Text.Length <> 0 And txtmidname.Text.Length <> 0 And txtlname.Text.Length = 0 Then
args.IsValid = True
Exit Sub
End If
If txtfname.Text = "" And txtmidname.Text = "" And txtlname.Text <> "" Then
args.IsValid = True
Exit Sub
End If
If txtfname.Text = "" And txtmidname.Text = "" And txtlname.Text = "" Then
args.IsValid = False
'CustomValidator2.ErrorMes
Exit Sub
End If
End Sub
Client script:
function validatingnames(source, args)
{
var firstname = document.getElementById('<
var midname= document.getElementById('<
var lname= document.getElementById('<
if (firstname.value.length != 0 && midname.value.length != 0 && lname.value.length == 0) {
args.IsValid = true;
return;
}
if(firstname.innerText =="" && midname.innerText =="" && lname.innerText !="") {
args.IsValid = true;
return;
}
if(firstname.innerText == "" && midname.innerText == "" && lname.innerText == "")
{
args.IsValid = false;
return;
}
}
in client validation it validating three fields correct but error message not displaying dynamic even i set display property Display="Dynamic".
ASKER
yes i did the same way as u said
client side code:
<script type="text/javascript" language="javascript">
function validatingnames(source, args){
var firstname = document.getElementById('< %=firstnam e.ClientID %>');
var midname= document.getElementById('< %=midname. ClientID%> ');
var lname= document.getElementById('< %=lname.Cl ientID%>') ;
if(firstname.value =="" && midname.value =="" && lname.value !="") {
args.IsValid = true;
return;
}
if(firstname.value =="" && midname.value =="" && lname.value !="") {
args.IsValid = true;
return;
}
if(firstname.value == "" && midname.value == "" && lname.value == "")
{
args.IsValid = false;
return;
}
}
</script>
aspx code:
<asp:TextBox ID="firstname" runat="server">
</asp:TextBox>
<asp:TextBox ID="midname" runat="server">
</asp:TextBox>
<asp:TextBox ID="lname" runat="server">
</asp:TextBox>
<asp:CustomValidator ID="cusval" runat="server" ErrorMessage="please fill f name mname or lname" Display="Dynamic" ClientValidationFunction=" validating names"></a sp:CustomV alidator>
<asp:Button ID="btn" runat="server" Text="submit"/>
</div>
still error message for customvalidator is not dynamic.
please see attached file more clear idea.
its validating correct.but when i fill the fname and midle name the error message should disappear right?
default.jpg
client side code:
<script type="text/javascript" language="javascript">
function validatingnames(source, args){
var firstname = document.getElementById('<
var midname= document.getElementById('<
var lname= document.getElementById('<
if(firstname.value =="" && midname.value =="" && lname.value !="") {
args.IsValid = true;
return;
}
if(firstname.value =="" && midname.value =="" && lname.value !="") {
args.IsValid = true;
return;
}
if(firstname.value == "" && midname.value == "" && lname.value == "")
{
args.IsValid = false;
return;
}
}
</script>
aspx code:
<asp:TextBox ID="firstname" runat="server">
</asp:TextBox>
<asp:TextBox ID="midname" runat="server">
</asp:TextBox>
<asp:TextBox ID="lname" runat="server">
</asp:TextBox>
<asp:CustomValidator ID="cusval" runat="server" ErrorMessage="please fill f name mname or lname" Display="Dynamic" ClientValidationFunction="
<asp:Button ID="btn" runat="server" Text="submit"/>
</div>
still error message for customvalidator is not dynamic.
please see attached file more clear idea.
its validating correct.but when i fill the fname and midle name the error message should disappear right?
default.jpg
Hi chakri27,
>>..still error message for customvalidator is not dynamic.
Do you referring custom dynamic error message that can be invoked via client script? The dynamic property that you defined here is referring visibility allocation based on reserved space.
Check this article for further details:
http://aspnetresources.com/blog/validation_easter_egg.aspx
>>but when i fill the fname and midle name the error message should disappear right?
No, it'll disappear only after your perform another round-trip to server(eg: click submit button again)
>>..still error message for customvalidator is not dynamic.
Do you referring custom dynamic error message that can be invoked via client script? The dynamic property that you defined here is referring visibility allocation based on reserved space.
Check this article for further details:
http://aspnetresources.com/blog/validation_easter_egg.aspx
>>but when i fill the fname and midle name the error message should disappear right?
No, it'll disappear only after your perform another round-trip to server(eg: click submit button again)
ASKER
Thank you for infomation.
i have one more question your saying it'll dispaly only after performs the round trip to server
then what is the use of using client validation function?i can just use servide right?
i want one suggetion from u: other than these text feild like fname lname mid name i also have three more text feilds i am using required field validators .i want all these fileds like fname ...+ 3 other txt fields should look uniform .other txt feilds error mesages diplays dynamic with out any round trip when i fill those fields .
is it any other way i can do it with out any round trip to server message dis appear when ever fname feild filled with text and loose focus?
bcause when user trying to submit at final some the feilds shows error message though filled with some value which is not look good other wise i caould have use all server side right?
should i change all server side?but my most preference is client side..
can u help me pleasee?
i have one more question your saying it'll dispaly only after performs the round trip to server
then what is the use of using client validation function?i can just use servide right?
i want one suggetion from u: other than these text feild like fname lname mid name i also have three more text feilds i am using required field validators .i want all these fileds like fname ...+ 3 other txt fields should look uniform .other txt feilds error mesages diplays dynamic with out any round trip when i fill those fields .
is it any other way i can do it with out any round trip to server message dis appear when ever fname feild filled with text and loose focus?
bcause when user trying to submit at final some the feilds shows error message though filled with some value which is not look good other wise i caould have use all server side right?
should i change all server side?but my most preference is client side..
can u help me pleasee?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks X com .for your information i really appreciated
If you're using textbox control, you should validate its "value" property instead of "innerText".
Try amend these lines:
if(firstname.innerText =="" && midname.innerText =="" && lname.innerText !="") {
args.IsValid = true;
return;
}
if(firstname.innerText == "" && midname.innerText == "" && lname.innerText == "")
{
args.IsValid = false;
return;
} if(firstname.innerText =="" && midname.innerText =="" && lname.innerText !="") {
args.IsValid = true;
return;
}
if(firstname.innerText == "" && midname.innerText == "" && lname.innerText == "")
{
args.IsValid = false;
return;
}
===================
Change To These
===================
if(firstname.value =="" && midname.value =="" && lname.value !="") {
args.IsValid = true;
return;
}
if(firstname.value == "" && midname.value == "" && lname.value == "")
{
args.IsValid = false;
return;
}