About
Pricing
Community
Teams
Start Free Trial
Log in
virtualmenon
asked on
2/22/2010
Combine funtionality of Requirefield Validator and Compare Validator
Hello there,
I have a textbox which needs to be checked for two things.
1) Whether its empty
2) For its datatype.(integer)
At the moment am using both compare and required field validators to do this. Can i combine this into one using custom validator or something
Thanks,
CPM
ASP.NET
ASP
9
1
Last Comment
Kaushal Arora
8/22/2022 - Mon
pradyahuja
2/22/2010
public static bool IsInteger(object numericValue)
{
if (numericValue == null)
{
return false;
}
else
{
int result;
return int.TryParse(numericValue.
ToString()
,
System.Globalization.Numbe
rStyles.In
teger,
System.Globalization.Numbe
rFormatInf
o.CurrentI
nfo,
out result);
}
}
if(String.IsNullOrEmty(txt
Box.Text) || IsInteger(txtBox.Text))
{
}
virtualmenon
2/22/2010
ASKER
Can i do this using validators??
ashraf882
2/22/2010
check String.IsNullOrEmty(txtBox
.Text) || IsInteger(txtBox.Text) using validator.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
virtualmenon
2/22/2010
ASKER
This is what am doing at the moment,
<asp:textbox id="tbAddAmount" runat="server" cssclass="editbox" columns="10" maxlength="20" />
<asp:requiredfieldvalidato
r id="rfvAmount" runat="server" text="*" errormessage="Enter valid Amount" controltovalidate="tbAddAm
ount" Display="Dynamic" validationgroup="vgLumpSum
" />
<asp:comparevalidator id="cvAmount" runat="server" text="*" operator="DataTypeCheck" type="Integer" errormessage="Enter valid Amount" controltovalidate="tbAddAm
ount" Display="Dynamic" validationgroup="vgLumpSum
" />
vora_bhaumik
2/22/2010
@virtualmenon is it working ???
Kaushal Arora
2/23/2010
function customvalidate(sender, args)
{
if(document.getElementById
('txtnum')
.value != "")
{
if(isNumeric(document.getE
lementById
('txtnum')
.value) == false)
{
args.IsValid = false;
return;
}
}
else
{
args.IsValid = false;
return;
}
args.IsValid = true;
}
function isNumeric(strValidate)
{
var blnResult = true;
var strNumber = new String(strValidate);
var i;
for(i=0;i<strNumber.length
;i++)
{
//alert(isNaN(strNumber.su
bstr(i,1))
;
if(isNaN(strNumber.substr(
i,1)))
{
blnResult = false;
}
}
return blnResult;
}
<asp:CustomValidator ID="CustomValidator2" runat="server" ClientValidationFunction="
customvali
date" ControlToValidate="txtnum"
ErrorMessage="Invalid Number.">*</asp:CustomVali
dator>
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
virtualmenon
2/23/2010
ASKER
@vora_bhaumik: Its working but i would prefer a custom validator doing both.
ASKER CERTIFIED SOLUTION
pradyahuja
2/23/2010
THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kaushal Arora
2/23/2010
Hi Virtualmenon:
I have given you t he same solution and that too using the custom validator. Anyways hope you got your answer.
{
if (numericValue == null)
{
return false;
}
else
{
int result;
return int.TryParse(numericValue.
System.Globalization.Numbe
System.Globalization.Numbe
out result);
}
}
if(String.IsNullOrEmty(txt
{
}