Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

custom validator asp.net

Posted on 2011-04-30
7
Medium Priority
?
303 Views
Last Modified: 2012-05-11
I have an ASP.NET form for entering Height and Weight . There are four fields text fields

txtfeet txtinches   and txtmeter  txtcentimeter

Only txtfeet and inches can be entered at the same time or txtcentimeter . One combination must be entered always

How to implement this using Validators, if applicable?
Thank you!

<asp:CustomValidator id="valCustom" runat="server"
    ControlToValidate="textfeet"
    ClientValidationFunction="ClientValidate"
        ErrorMessage="*Enter height in feet and inches or meters and centimeters" dispaly="dynamic">*
</asp:CustomValidator>

Open in new window

0
Comment
Question by:TonyReba
7 Comments
 
LVL 10

Expert Comment

by:Umar Topia
ID: 35499353
You have two options:

1) Either implement Custom Validator and then validate everything from the server side
2) Use your custom JavaScript function to check for the validations
0
 
LVL 3

Expert Comment

by:mortimer452
ID: 35500938
Just my opinion, but it would probably be better to use a RadioButton for the user to select "Feet/Inches" or "Centimeters."  Depending on the radio button selection, only show them one or the other.
0
 
LVL 9

Author Comment

by:TonyReba
ID: 35690806
Can somone review this code and tell me why is not working ?

protected void ServerValidate(object sender, ServerValidateEventArgs args)
    
    {
       //Case1 if both fields are empty Error 
        if (String.IsNullOrEmpty(txtfeet.Text) && String.IsNullOrEmpty(txtmeter.Text)) 
      args.IsValid = false;

      //Case2 if both fields are filled 
        else if (!String.IsNullOrEmpty(txtfeet.Text) && !String.IsNullOrEmpty(txtmeter.Text)) 
      args.IsValid = false; 
        else 
       args.IsValid = true;
        
    }

Open in new window

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!

 
LVL 14

Expert Comment

by:dejaanbu
ID: 35696783
protected void ServerValidate(object sender, ServerValidateEventArgs args)
   
    {
       //Case1 if both fields are empty Error
        if (String.IsNullOrEmpty(txtfeet.Text) || String.IsNullOrEmpty(txtmeter.Text))
      args.IsValid = false;

      //Case2 if both fields are filled
        else if (!String.IsNullOrEmpty(txtfeet.Text) && !String.IsNullOrEmpty(txtmeter.Text))
      args.IsValid = false;
        else
       args.IsValid = true;
       
    }
0
 
LVL 9

Author Comment

by:TonyReba
ID: 35755550
What changes did you make?
0
 
LVL 14

Accepted Solution

by:
dejaanbu earned 2000 total points
ID: 35755851
in the first if condition i have replaced && with ||
0
 
LVL 9

Author Closing Comment

by:TonyReba
ID: 35863233
ty
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 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