Avatar of virtualmenon
virtualmenon
 asked on

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.NETASP

Avatar of undefined
Last Comment
Kaushal Arora

8/22/2022 - Mon
pradyahuja

public static bool IsInteger(object numericValue)
        {
            if (numericValue == null)
            {
                return false;
            }
            else
            {
                int result;

                return int.TryParse(numericValue.ToString(),
                    System.Globalization.NumberStyles.Integer,
                    System.Globalization.NumberFormatInfo.CurrentInfo,
                    out result);
            }
        }
if(String.IsNullOrEmty(txtBox.Text) || IsInteger(txtBox.Text))
{
}
virtualmenon

ASKER
Can i do this using validators??
ashraf882

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

ASKER
This is what am doing at the moment,

<asp:textbox id="tbAddAmount" runat="server" cssclass="editbox" columns="10" maxlength="20" />

<asp:requiredfieldvalidator id="rfvAmount" runat="server" text="*" errormessage="Enter valid Amount"  controltovalidate="tbAddAmount" Display="Dynamic" validationgroup="vgLumpSum" />

<asp:comparevalidator  id="cvAmount" runat="server" text="*" operator="DataTypeCheck" type="Integer" errormessage="Enter valid Amount"  controltovalidate="tbAddAmount" Display="Dynamic" validationgroup="vgLumpSum" />

vora_bhaumik

@virtualmenon is it working ???
Kaushal Arora

function customvalidate(sender, args)
        {
            if(document.getElementById('txtnum').value != "")
            {
                if(isNumeric(document.getElementById('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.substr(i,1));
            if(isNaN(strNumber.substr(i,1)))
            {
                blnResult = false;
            }
        }        
        return blnResult;
    }

<asp:CustomValidator ID="CustomValidator2" runat="server" ClientValidationFunction="customvalidate" ControlToValidate="txtnum" ErrorMessage="Invalid Number.">*</asp:CustomValidator>
⚡ 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

ASKER
@vora_bhaumik: Its working but i would prefer a custom validator doing both.
ASKER CERTIFIED SOLUTION
pradyahuja

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

Hi Virtualmenon:

I have given you t he same solution and that too using the custom validator. Anyways hope you got your answer.