Link to home
Create AccountLog in
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
Avatar of pradyahuja
pradyahuja
Flag of Australia image

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))
{
}
Avatar of virtualmenon
virtualmenon

ASKER

Can i do this using validators??
check String.IsNullOrEmty(txtBox.Text) || IsInteger(txtBox.Text) using validator.
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" />

@virtualmenon is it working ???
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>
@vora_bhaumik: Its working but i would prefer a custom validator doing both.
ASKER CERTIFIED SOLUTION
Avatar of pradyahuja
pradyahuja
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Hi Virtualmenon:

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