RequiredFieldValidator doesn't work
-
Something must be wrong, as this is probably the most basic validation example ever - yet I still can't get it to work properly, and I actually suspect an error somewhere. Create an aspx page with a textbox, a requiredfieldvalidator and a button. Set the validator to validate the textbox and set its text property to "*". Run the project. Now, I would assume that clicking the textbox and then tabbing to the button would show me the text-message from the validator (the red *), indicating that I haven't entered data where I need. Guess what - it doesn't show up... However, clicking the field, entering data, tabbing away and then back, deleting the data again and then tabbing away again DOES show the red *. Because the validation of the textbox is handled from the onchange event. Try running this in the url field: javascript:void(alert(document.all.TextBox1.onchange)) - it will give you Message from webpage --------------------------- function anonymous() { ValidatorOnChange(event); } , whereas the javascript:void(alert(document.all.TextBox1.onchange)) will return null. Manually hooking the onblur event to the onchange event as so: javascript:void(document.all.TextBox1.onblur=document.all.TextBox1.onchange) will introduce somehow correct behaviour. Now - is it me who has failed to understand the simples of all validation scenarios - or is the requiredfieldvalidator broken. I hope it is the first option, but what the h... am I missing? Sample code:
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox1"
Text="*"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button"/>
</div>
</form>Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concert
-
Something must be wrong, as this is probably the most basic validation example ever - yet I still can't get it to work properly, and I actually suspect an error somewhere. Create an aspx page with a textbox, a requiredfieldvalidator and a button. Set the validator to validate the textbox and set its text property to "*". Run the project. Now, I would assume that clicking the textbox and then tabbing to the button would show me the text-message from the validator (the red *), indicating that I haven't entered data where I need. Guess what - it doesn't show up... However, clicking the field, entering data, tabbing away and then back, deleting the data again and then tabbing away again DOES show the red *. Because the validation of the textbox is handled from the onchange event. Try running this in the url field: javascript:void(alert(document.all.TextBox1.onchange)) - it will give you Message from webpage --------------------------- function anonymous() { ValidatorOnChange(event); } , whereas the javascript:void(alert(document.all.TextBox1.onchange)) will return null. Manually hooking the onblur event to the onchange event as so: javascript:void(document.all.TextBox1.onblur=document.all.TextBox1.onchange) will introduce somehow correct behaviour. Now - is it me who has failed to understand the simples of all validation scenarios - or is the requiredfieldvalidator broken. I hope it is the first option, but what the h... am I missing? Sample code:
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox1"
Text="*"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button"/>
</div>
</form>Do you know why it's important to make fast decisions? Because you give yourself more time to correct your mistakes, when you find out that you made the wrong one. Chris Meech on deciding whether to go to his daughters graduation or a Neil Young concert
RequiredFieldValidator is a server control and is not evaluated until a postback occurs. The onblur event is handled on the client.
only two letters away from being an asset