You've posted your question in the WebMatrix forum, so I asume you are using Web Pages? If so, the IsEmpty method will tell you if there is a value, and if there is, the IsInt method tells you whether it is an integer:
var Amount = Request["amount"];
if(IsPost){
if(!Amount.IsEmpty()){
//there's a value
if(Amount.IsInt()){
//if it's an int use AsInt to turn it into an int
int amount = Amount.AsInt();
....
Gunnar Calai...
Member
34 Points
31 Posts
Test if input field is numeric
Oct 30, 2011 04:32 PM|LINK
Hi!
I’m trying to validate an input field. The first is to test if it’s filled in which I do with
if (String.IsNullOrEmpty(Amount)) { … do something …
What is the most easy way to test if the field is numeric? Thank you in advance for your help.
Best regards
Gunnar Calais
sukumarraju
All-Star
17361 Points
3059 Posts
Re: Test if input field is numeric
Oct 30, 2011 04:39 PM|LINK
Regular Expressions
using System.Text.RegularExpressions; public static bool IsItNumber(string inputvalue) { Regex isnumber = new Regex("[^0-9]"); return !isnumber.IsMatch(inputvalue); }Application Architecture Guide 2.0
My Blog
Twitter
Rajneesh Ver...
All-Star
38354 Points
6989 Posts
Re: Test if input field is numeric
Oct 30, 2011 04:42 PM|LINK
Best is to use Requirefieldvalidator with CompareValidator so your page will not do postback.
<div> <asp:TextBox ID="numberTextBox" runat="server" /> <asp:RequiredFieldValidator ID="ReqValidator" runat="server" ControlToValidate="numberTextBox" ErrorMessage="Textbox should not blank"></asp:RequiredFieldValidator> <asp:CompareValidator ID="Comvalidator" runat="server" ControlToValidate="numberTextBox" Operator="DataTypeCheck" Type="Double" ErrorMessage="Value must be a number" /> </div>www.rajneeshverma.com
Keep Forums Clean || Use Alert Moderators.
tarunSaini
Contributor
2948 Points
985 Posts
Re: Test if input field is numeric
Oct 30, 2011 05:25 PM|LINK
use ajax filter control to set the targetcontrolid="textbox" validchars="0123456789"
Mikesdotnett...
All-Star
155659 Points
19987 Posts
Moderator
MVP
Re: Test if input field is numeric
Oct 30, 2011 08:14 PM|LINK
You've posted your question in the WebMatrix forum, so I asume you are using Web Pages? If so, the IsEmpty method will tell you if there is a value, and if there is, the IsInt method tells you whether it is an integer:
var Amount = Request["amount"]; if(IsPost){ if(!Amount.IsEmpty()){ //there's a value if(Amount.IsInt()){ //if it's an int use AsInt to turn it into an int int amount = Amount.AsInt(); ....Web Pages CMS | My Site | Twitter