Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KeyPressEventArgs

Status
Not open for further replies.

mrathi

Technical User
Oct 27, 2003
115
US
Hi,

I want to restric a user to enter only numbers in a textbox in asp.net. I use validation, however I also use postback, which basically does some arithmetic operation. If the user enters a valid number and hits TAB, my arithmetic works correct. If the user enters invalid number, before the validation is complete I get an error saying that the text cannot be converted to double? How can I rectify this?

Thanks
 
In the page_load event you could add:
Code:
this.txtNumber.Attributes.Add("onblur","return checkNumber();");

Then just create a client-side javascript function on the .aspx page. That way it won't postback.
Code:
function checkNumber()
{
  if(isNaN(Form1.txtNumber.value))
   {
	alert("Not a number");
	return false;
   }
}
 
See the Page.IsValid Property.

"Public ReadOnly Property IsValid As Boolean

Property Value
true if page validation succeeded; otherwise, false.

Remarks
For this property to return true, all validation server controls in the Page.Validators property must validate successfully. You should check this property only after you have called the Page.Validate method, or set the CausesValidation property to true in the OnServerClick handler for an ASP.NET server control that initiates form processing. These server controls include the Button, HtmlButton, HtmlInputButton, HtmlInputImage, ImageButton, and LinkButton classes."

Compare Code
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top