number validation for a variable

Posted by Sahoo under C# on 12/30/2011 | Points: 10 | Views : 2046 | Status : [Member] | Replies : 4
hi

can any body give me trick to validate a variable that it has some numeric value or not on server side.




Responses

Posted by: Self-innovator on: 12/30/2011 [Member] Bronze | Points: 25

Up
0
Down
In javascript u try this
function EnterOnlyNumbers(x) 

{
var len=x.value.length;
var s_charcode=0;
for(var s_i=0;s_i<len;s_i++)
{
s_charcode=x.value.charCodeAt(s_i);
if(!((s_charcode>=48 && s_charcode<=57)))
{
alert("Enter only numbers");
x.value="";
x.focus();
return false;
}
}
return true;
}
<asp:textbox id="txt1" ruat="server" onChange="EnterOnlyNumbers(this)">


if u want to do in server side make use of validation contols..Rangevalidator

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Sahoo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sahoo on: 12/30/2011 [Member] Starter | Points: 25

Up
0
Down
sorry sir

I need server side code only .

Sahoo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 1/2/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,

You can validate this using regular expression
string status="invalid";
if(Regex.IsMatch(txtbox1.text,@"^\d+$")
{
status="valid";
}

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sahoo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: dhirenkaunar-15094 on: 4/17/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

You can also validate using tryParse()


int a;
if(int.TryParse(txtbox1.Text,out a))
{
return "is a number";
}
else
{
return "is not a number";
}

Thanks & Rgards,
Dhiren Kumar Kaunar

Sahoo, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response