Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi All,
 
I am new to DotNet.
 
In a webpage I have one Textbox name as Status.I need to restrict the user to enter only Yes or No in the Textbox.
 
Help me out from this
 
Alternatively.
 
I can do with dropdown but there should be only textbox.
Thanks in Advance.
 
Subhash G.
Posted 18-Jul-11 17:09pm
Edited 18-Jul-11 19:25pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If the only possibilities are Yes and No, I would consider using a CheckBox rather than text for input.
  Permalink  
Comments
SAKryukov at 18-Jul-11 23:19pm
   
Finally! Totally agree. Request does not make any sense at all. (This silly thing comes second time already, from what I know. What are they thinking?) More exactly, please see my answer.
--SA
SAKryukov at 19-Jul-11 11:13am
   
Walt, please look at how much blaming I got for my answer. I am quite surprised that people protect their convenience of "meeting requirements" over the inconvenience to stand for better solution and convincing customers. I added few comments. What do you think?
--SA
Walt Fair, Jr. at 19-Jul-11 11:55am
   
Well, I think there's much to do about nothing. I said I would consider using a check box, etc., but not that I would demand it. If not meeting the requirements means he won't get paid, then I fully understand.
 
I can control my own code, but not others, so I figure that if they've been advised, then it's up to them to decide what makes more sense in their situation.
walterhevedeich at 19-Jul-11 22:43pm
   
This is a better comment, rather than telling OP that the request doesn't make sense.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can use Javascript[^] for validating your textbox. There are plenty of examples on the Internet about this.
 
[Update]
The link below is a good example of validating a textbox using Javascript.
http://kottawadumi.blogspot.com/2010/05/how-to-validate-textbox-using.html[^]
  Permalink  
v2
Comments
SAKryukov at 18-Jul-11 23:18pm
   
Why, why?! Request makes no sense at all!
Please see my solution.
--SA
walterhevedeich at 19-Jul-11 0:37am
   
I know we can use other controls for that, but since the user mentioned that he is aware of the other possibility(use of dropdown), but still insisted on using textbox, I might as well try to show him the possibility of doing it using a textbox. It might sound silly or doesn't make sense, but this can be a requirement from his client. And we can't deny the fact that some demanding clients make silly requirements, and insist on that. IMHO, if those requirements are doable, why not do what they want? After all, they will pay us in the end.
SAKryukov at 19-Jul-11 11:07am
   
I am quite surprised. Do you think "requirements" should be met? Who told you so :-)
OK, please see my comments to the comments on my solution.
It looks we have different attitudes to life.
--SA
walterhevedeich at 19-Jul-11 21:43pm
   
Do you think "requirements" should be met?
Let's put it this way. Why do you think "requirements" should not be met? How bout considering the fact that software development begins with requirements gathering? What makes it more important is that these requirements are the basis of what system you will create. Later on, the same requirements will be the basis by the Quality Analysts to create test cases and procedures to ensure that the system is doing what its supposed to do. Now, to answer your question, yes, it should be met.
subhash04573 at 19-Jul-11 1:47am
   
ya i too agree with you.
but the clien want's the textbox. wat should i do ?
walterhevedeich at 19-Jul-11 1:58am
   
See my updated answer. I have provided a link which illustrates how validating a textbox can be done in JavaScript.
SAKryukov at 19-Jul-11 11:08am
   
Please look at my other comments. This is where the skill to convince people comes into play. The only decent approach is to show better version to the customers, then it's easier to convince.
--SA
Pete O'Hanlon at 19-Jul-11 3:34am
   
So what if the request makes no sense> It's the clients requirements, not yours, so it's not very helpful telling somebody who's replied with the correct solution to the problem that they should look at your solution to see the way to do it - especially when your solution doesn't answer the problem asked.
Rajesh R Subramanian at 19-Jul-11 4:42am
   
Very well said. It's funny how almost every comment he posts ends with "see my answer".
SAKryukov at 19-Jul-11 11:10am
   
I do this only when and because I'm sure I'm right. When I'm not, I learn from others. Now, please see my comments about meeting requirements. I maintain that in many cases "meeting requirements" is simply dishonest. "Do not make harm" should be the dominating principle.
--SA
subhash04573 at 19-Jul-11 2:01am
   
Thanks, this will solve the issue
subhash04573 at 19-Jul-11 2:01am
   
tnks
Al Moje at 20-Jul-11 21:49pm
   
I had been encountered this situation when I am developing a Water Billing Application, whereas my Client told me to do this way… But I insisted him and replied ‘This is not good’. Then he replied too ‘Who should be the right?’ Then I’d remember my college days where my professor/teacher in marketing told us ‘Remember the Customer is Always Right’. Then it comes to my mind if ever I negate to what he told to do so… I will never pay…
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Use Radio Button or Check Box. Because you have the option as Yes or No. that will be better instead of Textbox.
  Permalink  
Comments
SAKryukov at 3-Aug-11 21:42pm
   
My 5. I don't understand this craze about TextBox. So many people started to protect this absurd design and down-vote (please see my solution where I also suggested Radio Button or Check Box).
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Yes or No TextBox? Makes no sense at all!
 
1) Make two radio buttons in the same group: "Yes" and "No".
 
or, better yet
 
2) Make one check box "Confirm blah-blah...". Checked means "Yes", unchecked means "No".
 
That's it.
 
—SA
  Permalink  
v2
Comments
Walt Fair, Jr. at 18-Jul-11 23:22pm
   
Yes, that's what I would do. Simple and no validation needed.
SAKryukov at 18-Jul-11 23:26pm
   
Absolutely. Thank you, Walt.
--SA
Mohammad A Rahman at 18-Jul-11 23:39pm
   
I agree with SA. :)
SAKryukov at 19-Jul-11 0:24am
   
Thank you, Mohammad.
--SA
Ramalinga Koushik at 19-Jul-11 0:27am
   
Good Call SA.
SAKryukov at 19-Jul-11 11:05am
   
Thank you, Ramalinga.
I'm quite surprised by two comments below.
--SA
DaveAuld at 19-Jul-11 1:59am
   
Having to enter text means the user has made a conscious decision to enter a value, rather than clicking blindly, this can be important when dealing in financial system e.g. stock trading etc. See another example I have experienced here; http://www.codeproject.com/Lounge.aspx?msg=3962629#xx3962629xx
So, the best option from a programmers point of view, is not always the way you can do it (if the client says No)
SAKryukov at 19-Jul-11 11:04am
   
Well, thank you for your note, but I disagree. Just typing "yes" or "no" provides no incentive for the user to think more thoroughly, will only create irritation (and probably denial to think at Silly Things). I feel you only argument "in principle", not picturing the user behavior. I would kindly suggest you think again.
--SA
Pete O'Hanlon at 19-Jul-11 3:28am
   
While we may think that there are better solutions, it's arrogant of us to assume that we are right. Quite often nonsensical requirements come from requirements, and no matter how much the poster points out a better way of doing something, the client sticks to their opinion.
SAKryukov at 19-Jul-11 11:01am
   
OK, it looks we have different attitude to the whole life.
 
I ***will not*** meet requirements which I think are foolish, no matter how much it costs me.
 
Sometimes, meeting requirements is dishonest, a crime, whatever it can be, you name it. And I pay for my "arrogance" well enough, believe me. I'm well over 20 years in development, and nobody was able to break my simple principles, can you hear that? In most cases, I managed to convince people to do right things. I ***will not make harm*** like creating a "Yes"/"No" text box. I will not even well tolerate is I make my own mistake in requirements; and some developer who understand it will implement it "as is" without pointing out my mistake. In practice, everyone argues with me, to best of my knowledge; and this is correct.
 
--SA
walterhevedeich at 19-Jul-11 21:55pm
   
OK, it looks we have different attitude to the whole life.
...In practice, everyone argues with me
Don't you think these are complementary? :) Well, if everyone argues with you, then there must be something wrong. Probably them? And you are always right? :), Well I don't know.
SAKryukov at 19-Jul-11 23:35pm
   
Trying to catch me on controversy? Let's test it. You rip those two phrases from the context, that's it. Everyone can do such tricks, but let's be serious.
 
First phrase is my reaction to Pete's comment. It looks the attitudes are different, that's it. This is related to the attitude to requirements. I still think that not all requirements should be met.
 
Second phrase... I need to check it up what I really said, but the idea is: if there is a conflict of opinions, my task is to make a consensus. If a customer's requirement looks like an obvious mistake to me, I should try to convince them. Usually I make them agree. There was situations where I failed to convince them, but usually I was not sure I was 100% right. The situation with this question is different. I'm 100% sure text box is absolutely wrong.
 
Of course I cannot guarantee, nobody can. This is matter of principle. If somebody orders you to break the law, what will you do? I can make mistakes, sometimes big mistakes as everyone can. But I'm responsible for them. Doing stupid thing with the excuse "because customer told me to do so" (or "boss") is irresponsible. Very typical in companies. But this is one the big reasons why projects are failing and companies go out of business.
 
--SA
Pete O'Hanlon at 20-Jul-11 6:51am
   
You're right, we do disagree. If a client has a legitimate reason to want a Yes or No textbox then no matter how much we may think they are wrong, it is their business not ours. They know their business processes much better than we do. Making a yes/no box does not do harm and the user has said they have a requirement to do this. Frankly, I would drag you off a project if you refused to implement a perfectly legal requirement just because you disagreed with it - if I didn't, I'm damned sure my clients would kick up a fuss and demand that I do it.
 
The criminality argument is specious and disingenuous at best. Legality and adherence to legality has nothing to do with business requirements - it has to do with corporate responsibility.
 
In your answer here, you have told the OP that there are only two possible solutions to his problem, and that is the real problem. You come on very strong in the forums, telling people to go to your answer, and that can give people the opinion that only you have the answers, especially when you tell people to see your answer, as though that is the definitive answer. Yet in this case, you have completely failed to answer the OPs question. You've answered the question you think should have been asked.
 
Don't get me wrong - I think you generally give good answers. I just think you have to dial it back a bit and possibly stop looking like you are constantly hunting to be at the top of the "Experts" list (and that does mean stop telling people who have given perfectly valid answers to see your answer).
SAKryukov at 19-Jul-11 11:16am
   
Also, you should understand that OP's relationship with customer is not my concern. I post advice on UI, etc., not on what to do with them. We all chose the questions to answer, right, don't try to help everyone. (Sometimes it simply would take too much time.) This is the same thing.
--SA
Nagy Vilmos at 20-Jul-11 7:15am
   
I think you're assumption that you are *always* right is, at best, misguided.
There are plenty of good reasons to not want a checkbox - one would be it can only record a single action that is it is clicked rather then an option has been taken yes or no - and maybe the use of radio buttons does not fit in with the general UI/UX - I try to avoid them as they do not generally give a clean flow - so having a text field is appropriate. That does not mean it had to be 'free text' - you assumed that - it could respond to Y/N to set the text value, maybe have a toggle on space or mouse click.
 
There are a LOT of good designs for an option that do not use a checkbox. You do not have the full design, so do not make assumptions.
ashika menon at 20-Jul-11 7:35am
   
you r great SA....
AspDotNetDev at 20-Jul-11 13:53pm
   
Actually, a yes/no textbox can make perfect sense. For example, a webpage may require you to type in "yes" or "no" on a legal document as a form of signature. Making it harder for the user (and, in this case, more deliberate) ensures that they will not accidentally click a checkbox. This makes the input typed into a yes/no textbox more legally actionable than a checkbox (i.e., it is easier to believe "I accidentally clicked the checkbox" than "I accidentally typed 'yes' into the textbox"). And that is just one example of where a yes/no textbox would make sense; I'm sure there are many other uses for such a control.
SAKryukov at 20-Jul-11 21:47pm
   
I disagree with the very first principle you're trying to use to save the idea: "harder" means "more conscious". This is a pretty populate view, but I think nobody proved it really works; did you?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
 
This sample used AjaxControlToolkit.
 
Try this if could help:
 
  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Auto">
    </asp:ScriptManager>
    <div>
        <table width="100%">
            <tr align="left">
                <td>
                    Status:&nbsp;
               
                    <asp:TextBox ID="txtStatus" runat="server" Width="3%" MaxLength="3"></asp:TextBox>
                     
                    <cc1:FilteredTextBoxExtender ValidChars="YesNo" FilterMode="ValidChars" 
                    TargetControlID="txtStatus"
                        ID="fvtxtStatus" runat="server">
                    </cc1:FilteredTextBoxExtender>
                </td>
            </tr>
            <tr>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </td>
            </tr>
        </table>
        
    </div>
    </form>
</body>
 
 
You must have a copy of AjaxControlToolkit.dll. You may google it to have a copy...
 
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
 
Regards,
 
Algem
  Permalink  
v2
Comments
SAKryukov at 18-Jul-11 23:19pm
   
Why, why?! Request makes no sense at all!
Please see my solution.
--SA
Nagy Vilmos at 20-Jul-11 7:17am
   
He answered the question, you didn't
Al Moje at 18-Jul-11 23:40pm
   
I agree with asp:RegularExpressionValidator if the
solution has no reference to Ajax, but if in cases
that the application is having reference to Ajax,
the asp:RegularExpressionValidator does not work.
I had been try this...
a referrence library to Ajax,
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You could try asp:RegularExpressionValidator and ^[YES|NO]*$ as RegularExpression for the validator.
 
and please visit this site,
 
RegularExpressionValidator[^]
  Permalink  
Comments
SAKryukov at 18-Jul-11 23:20pm
   
One more... Why, why?! Request makes no sense at all!
Please see my solution.
--SA
Al Moje at 18-Jul-11 23:31pm
   
I agree with asp:RegularExpressionValidator if the
solution has no reference to Ajax, but if in cases
that the application is having reference to Ajax,
the asp:RegularExpressionValidator does not work.
I had been try this...
a referrence library to Ajax,
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Is it using Text box For Yes or No?

1) Make two radio buttons in the same group: "Yes" and "No".

2) Make one check box , Checked means "Yes", unchecked means "No".
  Permalink  
Comments
SAKryukov at 19-Jul-11 23:54pm
   
Why would you simply repeat my answer almost one-to-one? By the way, take a look how many down-votes I got. What do you think?
--SA
ashika menon at 20-Jul-11 2:58am
   
nop.............i jst gave the answer.......... not coopied from u...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 19 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100