I have a text box that a user is to enter a quantity. I have validation working so that they have to enter an integer, however, the form will still submit if the text box for quantity is left blank. The field isn't required, so how do I check to make sure that the user doesn't submit a blank value on top of making sure they enter an integer? This is what I have so far...
<
asp:TextBox ID="SpclBioQty" runat="server" Columns="5" Width="33px"></asp:TextBox><asp:CompareValidator ID="SpclBioQtyValidator" runat="server" ErrorMessage="*Data must be a number" ControlToValidate="SpclBioQty" Type="Integer" Operator="DataTypeCheck"></
asp:CompareValidator>
Never make important decisions on a Monday!
![]() |
0 |
![]() |
You are missing
ControlToCompare Attribute of Compare Validator.
Please remember to click “Mark as Answer” on the post that helps you
Best Regards
Brij Mohan
http://www.dotnetglobe.com
![]() |
0 |
![]() |
Changed my code to this (staight out of my asp.net 3.5 book) and it still lets the form submit with an empty value... however, if I try to enter -2, it does catch that this value is less than zero. What am I missing?
<
asp:TextBox ID="PocketFolderQty" runat="server" Columns="5" Width="33px"></asp:TextBox><asp:CompareValidator ID="PocketFolderQtyValidator" runat="server" ErrorMessage="*" ControlToValidate="PocketFolderQty" Operator="GreaterThan"ValueToCompare="0"> </asp:CompareValidator>
Never make important decisions on a Monday!
![]() |
0 |
![]() |
From http://msdn.microsoft.com/en-us/library/db330ayw(VS.71).aspx
Note If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to prevent the user from skipping an input control.
NC...
![]() |
0 |
![]() |
Argh... well, at least I understand now. My silly book says it will work. Thank you NC01... saved me a hour of beating my head on the desk trying to figure out why. Unfortunately, the field isn't a required field, so I don't want to use a requiredfieldvalidator. So, I guess they'll be able to enter a "NULL" value if they don't enter a qty...
Never make important decisions on a Monday!
![]() |
0 |
![]() |
You can use javascript to overcome this issue the code will be something like below
in the code behind addButton1.Attributes.Add(
"onclick", "return func()");and in the .aspx page include the following script
<
script language="javascript">function func(){
if (document.getElementById("TextBox1").value == ''){
alert('Blank Not Allowed!')return false;}
}
</script>
![]() |
0 |
![]() |
Why would it necessarily have to be a required field? If you want to require a value in it, just add a RequiredFieldValidator. All that's going to do is not let them submit the page without a value there. They could just place a "0" in it, in fact you could default it to "0" if empty like this:
<asp:TextBox ID="PocketFolderQty" runat="server" Columns="5" Width="33px" onfocus="if ( this.value == '0' ) {this.value = '';}" onblur="if ( this.value.length <= 0 ) {this.value = '0';}"></asp:TextBox>
Note that I attached event handlers to the element in the HTML declaration. This will work, but will produce a compiler warning. The proper way to do this for a server control is in the Page_Load handler of the server-side code like this:
PocketFolderQty.Attributes.Add("onfocus", "if ( this.value == '0' ) {this.value = '';}");
PocketFolderQty.Attributes.Add("onblur", "if ( this.value.length <= 0 ) {this.value = '0';}");NC...
![]() |
0 |
![]() |
hmm interesting... I dont' know much about javascript...
getting a message saying Expected';' on this line of code... alert('Blank Not Allowed!')return false;
Never make important decisions on a Monday!
![]() |
0 |
![]() |
check this
alert('Blank Not Allowed!');
return false;
![]() |
0 |
![]() |
Did you try the code that I posted? That would seem to me to be a heck of a lot more unobtrusive than validating the TextBox twice.
NC...
![]() |
0 |
![]() |
I just want to point out that you could possibly bypass the text box all together (never gains focus) by using a mouse which would prevent onfocus and onblur from being executed. A javascript function that executes when the button is clicked (onClick) or when a form is submitted (onSubmit) could be created that checks to see if the textbox is empty, and if so, load it with a zero (0)
If only everything was easy as “Hello World”
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
![]() |
0 |
![]() |
vista7.. well, it doesn't throw an error anymore, but it doesn't prevent the space from inserting either. Here's my code from the code behind... did I add it wrong?
Protected Sub PocketFolderSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PocketFolderSubmit.ClickPocketFolderSubmit.Attributes.Add("onclick", "return func()")dsPocketFolderInsert.Insert()
End Sub
Never make important decisions on a Monday!
![]() |
0 |
![]() |
NC01 - Yes, tried it too here's my code from the code behind.. but nothing happenened when I clicked the button without entering a value
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadPocketFolderQty.Attributes.Add(
"onfocus", "if ( this.value == '0' ) {this.value = '';}")PocketFolderQty.Attributes.Add(
"onblur", "if ( this.value.length <= 0 ) {this.value = '0';}")End Sub
Never make important decisions on a Monday!
![]() |
0 |
![]() |
All that code does is take care of when the TextBox gains or loses the focus. If you want the TextBox filled on the button click, add this to the Page_Load handler also.
Dim clickHandler As String = String.Format("if ( document.getElementById('{0}').value.length <= 0 ) {document.getElementById('{1}').value = '0';}", PocketFolderQty.ClientID, PocketFolderQty.ClientID)
Button1.Attributes.Add("onclick", clickHandler)NC...
![]() |
0 |
![]() |
change the if condition to
if
((document.getElementById("TextBox1").value == ' ')||(document.getElementById("TextBox1").value == ''))
![]() |
0 |
![]() |