0

I have custom application page, and I need some custom client side validation. After spending hours on trying to make asp.net CustomValidator to work, I googled a lil bit and found few posts, that advise to use InputFormCustomValidator, instead of asp.net based CustomValidator. From what I see there is no big difference between two controls ( well maybe 2 or 3 additional properties that InputFormCustomValidator supports ).

I have a textbox, and jQuery datepicker, when user clicks on submit button I just want to check what option is selected on the dropdownlist on the form, and than based on the selected value, I need to check if textbox has any input. This is what I have so far.

<asp:TextBox ID="txtProjectStartDate" runat="server" Width="75" />
<SharePoint:InputFormCustomValidator ID="InputFormCustomValidator1" runat="server" ControlToValidate="txtProjectStartDate"
 ClientValidationFunction="ValidateProjectStartDate" ErrorMessage="*" EnableClientScript="true" />

and this is custom validation function

function ValidateProjectStartDate(sender, args) {

    var projectType = $('select[id*=ddlProjectType]').val();

    if (parseInt(projectType) != 1) {

        var startDate = $('input[type=text][id*=txtProjectStartDate]').val();

        if (startDate.length == 0) {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }
    }
}

I have checked, there is no js error on my page, but custom validator doesn't fire. I have even tried to handle submit button click event, and programmatically force Validation, but no luck so far. If you've successfully used asp.net CustomValidator control in similar scenarios, I'd be glad to switch to CustomValidator as well.

Can anybody help me to make this code work ?

1 Answer 1

2

I figure this out, the problem was, I was testing with empty values, more exactlly I was left TextBox blank, and click on Submit button. But neither CustomValidator nor InputFormCustomValidator don't work with Empty values, unless ValidateEmptyText property is set to true. So all I was needed is to set ValidateEmptyText property to true. Now it works as expected. Hope this helps somebody else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.