Tuesday, January 18, 2011

RegularExpressionValidator autocomplete enter key problem in firefox

I had some problems with the regularexpressionvalidator in firefox today. When I selected a value from the browsers autocomplete box, the field was validated to false.

Autocomplete in firefox

The validator throws an error

The RegularExpressionValidator checks if the insert email address has the right format. If not than the validator outputs an error. In my case when I hit the enter key in the autocomplete box for the field, the validator message appeared, but when I hit the tab key all was fine.

I digged a little into the javascript and found out that when I pressed Enter the value for the field to check in forefox was only the first character of the value entered. Therefore this was not a valid email address

Then I found this post, that describes that this is a firefox bug

The only solution for this problem seems to override the Microsoft core ValidateTextBoxOnKeyPress function somewhere in your page and check if the function is called in firefox. (I used here the JQuery browser method )

1:  function ValidatedTextBoxOnKeyPress(event) {  
2:    if (event.keyCode == 13 && !$.browser.mozilla) {  
3:      ValidatorOnChange(event);  
4:      var vals;  
5:      if ((typeof(event.srcElement) != "undefined") && (event.srcElement != null)) {  
6:        vals = event.srcElement.Validators;  
7:      }  
8:      else {  
9:        vals = event.target.Validators;  
10:      }  
11:      return AllValidatorsValid(vals);  
12:    }  
13:    return true;  
14:  }  


The probably easier variant is to force Firefox to hide the AutoComplete popup by adding the property autocomplete="off" to the input field.


<input type="text" autocomplete="off" value=".." name=".." />


or in ASP


<asp:TextBox runat="server" autocomplete="off" Text"..." />

2 comments:

Imran Shaikh said...

Many thanks Baris. This is exactly I was looking for.

Swati Dane said...

Thanks a lot !