Issue with RegularExpressionValidator - c#

I have a problem with the RegularExpressionValidator. This seems to be issues with the validator itself.
The problem occurs with Firefox because it has an autocomplete dropdown, however it could also have the same issue on other browsers.
Problem 1
If I focus inside a textbox, Firefoxes autocomplete displays. If I highlight a valid entry but press enter rather than say tab, a validation error occurs, even though the value is correct.
If I tab outside the textbox the message resets.
Problem 2
If I repeat the process that caused problem 1, by focusing back into the textbox letting firefox display auto complete and pressing enter again, the same problem occurs however this time the validator is not cleared when focus moves outside the textbox and the error message remains visible.
Any advice?

Your issue is related to the fact that you're pressing the Enter key, which causes a postback. When the page posts, it sends the state of each control at the time you pressed Enter, so the value you chose from the autocomplete dropdown wasn't populated yet. My recommendation would be to disable the Enter key when the textbox has the focus.
You can find an example of that here.

Related

Silverlight DatePicker text entry requires enter key

In a Silverlight web app that I'm working on, there's a DataGrid with a column that displays as a DatePicker. The DatePicker gives the option to change its value by typing in the box, or by clicking on the calendar button and selecting the date. In the case where text is entered, if the cell loses focus then the entered value is lost and the old value is displayed. If text is entered and then the Enter key is pressed, then the value is kept when the cell loses focus.
Also curious is that other editable cells in this grid already keep their information without needing to first hit enter.
Is there a way to have the value kept without having to press the Enter key? There's obviously some sort of built-in event happening when the Enter key is pressed in order for this to work. Maybe I can use the LostFocus event to fire whatever the Enter key is using? What's going on here?
You may want to try using the keydown event and trap all keystroke then flush the data to where this control was bound.

Form only saving when inputs lost focus

I'm using a form (FormView) with databinding (ObjectDataSource) and all my input fields are bound by using '<%# Bind("field") %>'.
Everything works fine, but I have two problems (which I found various hints about like using this.Validate() or .EndEdit() - but none seem to work):
Entries are only saved after leaving the input field so it looses focus
Let's say I have a textbox with an ID of Name and enter "George". When I would tab to the next textbox or when I click somewhere else and click save - everything is saved. But when I keep the focus in the textbox the value is not saved. Why is this happening? What magic can I use to circumvent this (JavaScript to the rescue?).
I set a textbox's field value (element.value) via Javascript (upon selecting something in a combobox).
The same problem as above applies, only when I give the textbox focus and tab out the value is saved. This creates the problem that I only want the user to choose something in the combobox (the textbox is updated accordingly) and move on - I don't want the user to click into the textbox afterwards and tab out again.
Edit:
The second problem I resolved now by setting the focus onto my textbox via Javascript (textbox.focus();) and right after set the focus back to the combobox (combobox.focus();) and that does the trick - this seems fairly hackish to me, doesn't it?
I'm assuming this is fairly common, but my mighty Google fu hasn't help me find a simple solution.
A similar issue can crop up in Winforms development when working with DataGridView controls. I typically attach some logic to the submit button's Click event to cause the DataGridView to validate. I suspect a similar solution would work for you here.

DataGridView: Bailing out of cell validation

I'm handling CellValidating and setting e.Cancel = true if the data in the cell is invalid. This almost gets me where I want to be, but the problem is if the user has some invalid data in the cell, the rest of the UI is essentially off-limits until they fix the error or press Esc.
Since pressing Esc might not be intuitive to certain users, they may find it frustrating that they can't e.g. click on the "Back" button I have on the form to leave the screen altogether. (In such a scenario, their change-in-progress should be discarded as if they had pressed Esc.)
Any ideas on how to achieve this? I do like that they aren't allowed to start editing other cells without fixing errors in their current cell, but I'd prefer they still be able to press the "Back"/"Cancel" buttons on the form.
Thanks in advance!
You could set the CausesValidation property to false.

C# UserControl Validation

I have a UserControl with a Tab Control containing three tabs. Within the tabs are multiple controls - Datetimepickers, textboxes, comboboxes.
There is also a Save button which when clicked, calls this.ValidateChildren(ValidationConstraints.Enabled) Now, I click save and a geniune validation error occurs. I correct the error and then click save again - valdiation errors occur on comboboxes on a different tab. If I navigate to this tab and click save, everything works fine. How can this be? I haven't changed any values in the comboboxes so how can the fail validation then pass validation?
The comboboxes are bound to a dataset with their selectedValue and Text set. I just don't understand what is happening here.
This behaviour also occurs for some textboxes too. The validation rule is that they have to be a decimal - the default value is zero, which is allowed. The same thing happens, they fail validation the first time - I make no changes, click save again and they pass validation.
EDIT:
The error is not a Framework error - the error is the one I provide during my validation routine. e.g Select a valid drop down option.
I stepped through the process and the sometimes the combobox values are the default values other times the selectedvalue is the default but the text is null. I don't see how this can happen if I'm not changing anything in the combobox.
If you need any further information please let me know
thanks
Barry
The following is a quote from MSDN:
"Controls contained in a TabPage are not created until the tab page is shown, and any data bindings in these controls are not activated until the tab page is shown."
So I'm guessing that if before you press save the first time, you make sure that you've clicked on each tab at least once, it'll work as expected, but if you don't look at all tabs before saving, it fails?
Have you checked that the default value is not null ?

Override tab behavior in WinForms

I have a UserControl that consists of three TextBoxes. On a form I can have one or more or my UserControl. I want to implement my own tab behavior so if the user presses Tab in the second TextBox I should only move to the third TextBox if the the second TextBox has anything entered. If nothing is entered in the second TextBox the next control of the form should get focus as per the normal tab behavior. If the user hasn't entered anything in the first or second TextBox and the presses tab there is this special case where a control on the form should be skipped.
By using the ProcessDialogKey I have managed to get it work kind of ok but I still have one problem. My question is if there is a way to detect how a WinForms control got focus since I would also like to know if the my UserControl got focus from a Tab or Shift-Tab and then do my weird stuff but if the user clicks the control I don't want to do anything special.
As a general rule, I would say overriding the standard behavior of the TAB key would be a bad idea. Maybe you can do something like disabling the 3rd text box until a valid entry is made in the 2nd text box.
Now, having said this, I've also broken this rule at the request of the customer. We made the enter key function like the tab key, where the enter key would save the value in a text field, and advance the cursor to the next field.
I don't think there's a built-in way that you could do it. All of the WinForms focus events (GotFocus,LostFocus,Enter,Leave) are called with empty EventArgs parameters, which will not give you any additional information.
Personally, I would disable the third textbox, as Rob Thomas said. If you're determined to do this, though, it wouldn't be difficult to set up a manual (read: hackish) solution. Once the tab key is pressed (if the focus is on the second textbox), set a variable inside your form. If the next object focused is then the third textbox, then you know exactly how it happened.
The reason for this odd tab behavior is all about speed in the input process. It was really good to get some input, I hadn't thought about disabling a textbox but that could actually work. But using the Enter key to accept the input hadn't even crossed my mind. That will work so much better. The user can enter the numbers and then press enter to accept the input and the next possible textbox will be the active one. It's like having the cake and eating it too, The speed factor is there since when using the enter key no unnecessary tabing must be done to get to the correct field and using the enter key next to the numeric keyboard makes it really smooth.
Thanks for the input!
I agree with DannySmurf. Messing with the tab order might give you hell later on if the requirements for the application change.
Another thing that you could do is to implement some kind of wizard for the user to go through.
Better than disabling controls, try monkeying around with TabStop - if this is false, the control will be simply skipped when tabbing.
I'd also suggest that the Changed event of the TextBox is the place to be updating TabStop on the other controls.
I've done something similar to this with a login control, where users could enter either a username or an email address (in separate fields), plus their password, and tabStop is what I used to get the job done.

Categories