[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Client vallidation for CustomValidator control

Posted on 2009-04-21
6
Medium Priority
?
264 Views
Last Modified: 2012-05-06
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".
0
Comment
Question by:chakri27
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 24201495
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;

                   }
0
 

Author Comment

by:chakri27
ID: 24202530
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
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 24212739
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)

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chakri27
ID: 24229743
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?

0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 2000 total points
ID: 24257755
Hi chakri27,
Sorry for delay response due to my heavy routine.

>>..what is the use of using client validation function?i can just use servide right?
You can use client validation for those validation that can be performed before reach server for further processing. However, i'll suggest you to validate in both side if the validation is crucial for essential data such as mortgage system. But, this have to depend your system design. You can see more clarifications regarding server or client validation here:
http://weblogs.macromedia.com/cantrell/archives/2003/09/validation_-_cl.html

>>..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 .
For your information, the custom validation's error message'll not validate automatically like requiredfieldvalidator control. Its error message only be displayed after you perform client script validation under some specify circumstance (such as button's onclick event).

>...s 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?
Yes, you can do that via javascript event. Perhaps do that under textbox's onblur event to fire the client script validation as proposed earlier.


0
 

Author Comment

by:chakri27
ID: 24257797
thanks  X com .for your information i really appreciated
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question