Link to home
Start Free TrialLog in
Avatar of chakri27
chakri27Flag for United States of America

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_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)

        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.ErrorMessage = " * 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('<%=txtmidname.ClientID%>');

     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".
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Hi chakri27,
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;

                   }
Avatar of chakri27

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('<%=firstname.ClientID%>');

     var midname= document.getElementById('<%=midname.ClientID%>');

     var lname= document.getElementById('<%=lname.ClientID%>');
     
   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="validatingnames"></asp:CustomValidator>
    <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)

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?

ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks  X com .for your information i really appreciated