Link to home
Start Free TrialLog in
Avatar of Starr Duskk
Starr DuskkFlag for United States of America

asked on

Validation Error showing on reveal of textbox instead of on submit only

I have a checkbox that enables and disables the validation of a textbox when checked. I am doing this on client side and want everything to be on client side.

I am attempting to use jquery to accomplish what I need to do. I have written most of the code already and it works mostly how I want it to, except for when I first activate the textbox it throws up the validation error. I only want it to validate on submission.
User generated image
Here is my code:
    var idEmail = {
        txtEmailNotification: "<%= txtEmailNotification.ClientID %>"
    }
    var idEmailVal = {
        valRegExpEmailNotification: "<%= valRegExpEmailNotification.ClientID %>"
    }
    var idEmailReqVal = {
        valEmailNotification: "<%= valEmailNotification.ClientID %>"
    }
$(document).ready(function functionCheckEmail() {
    //check if checkbox is checked on page load
    if ($('#chkIsEmailNotices').is(':checked')) {

        $("#" + idEmail.txtEmailNotification).show(); //shows if checked
        ValidatorEnable($("#" + idEmailVal.valRegExpEmailNotification)[0], true); //shows validators
        ValidatorEnable($("#" + idEmailReqVal.valEmailNotification)[0], true); //shows validators

    } else {

        $("#" + idEmail.txtEmailNotification).hide(); //hides if not checked
        ValidatorEnable($("#" + idEmailVal.valRegExpEmailNotification)[0], false); //hides validators
        ValidatorEnable($("#" + idEmailReqVal.valEmailNotification)[0], false); //hides validators
    }
    $('#chkIsEmailNotices').click(functionCheckEmail);
});

Open in new window

Through various resources I have found out how to hide/show a textbox with validation using a checkbox, but if anyone has a better solution that allows me to accomplish my task client side then that would be great. Hopefully there is a quick solution to this that I am not seeing. Thanks!
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

Could you please show us the core of ValidatorEnable function?
ASKER CERTIFIED SOLUTION
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Starr Duskk

ASKER

@Zakaria Acharki I am not sure what you mean by core of the ValidatorEnable, but in my main post I included all the javascript that I am using. Some other code that I felt might be relevant is the code in front:
<div class="stylPaddingTop">
                            <asp:TextBox ID="txtEmailNotification" CssClass="" runat="server"></asp:TextBox>
                        </div>
                        <div>
                            <asp:RequiredFieldValidator ID="valEmailNotification" runat="server"></asp:RequiredFieldValidator>
                        </div>
                        <div>
                            <asp:RegularExpressionValidator ID="valRegExpEmailNotification" runat="server"></asp:RegularExpressionValidator>
                        </div>

Open in new window

The code behind:
    Protected Sub LoadValidators()

        Try
            btnSaveNotifications.ValidationGroup = NotificationsValidationGroup
            valEmailNotification.ValidationGroup = NotificationsValidationGroup
            val.RequiredFieldValidate(valEmailNotification, "txtEmailNotification", "Email Required", "*Required")
            val.RequiredField(lblEmailNotifications)

            valRegExpEmailNotification.ValidationGroup = NotificationsValidationGroup
            val.ValidExpressionEmail(valRegExpEmailNotification, "txtEmailNotification", "Email", "*Please enter a valid email")
            '1/7/2019 KRERT #1210 My Account: changed to true
            valEmailNotification.Visible = True
            valRegExpEmailNotification.Visible = True ' ShowHideIsEmailNotices '1/4/2019 KRERT #1210 My Account show all the time, and handle with javascript

        Catch ex As Exception
            SendError(ex.Message)
        End Try
    End Sub

    Protected Function HandleSave(ByVal ValidationGroup As String) As Boolean
        Dim rdh As New RiverDogHandler
        If chkIsEmailNotices.Checked = False Then
            valRegExpEmailNotification.Enabled = False
            valEmailNotification.Enabled = False
        End If
        Dim bSuccess As Boolean = rdh.HandleSave(Me.LoggedInUserProfileId, ValidationGroup, Page)
        'sets validators as true if checked
        valRegExpEmailNotification.Enabled = True
        valEmailNotification.Enabled = True

        If bSuccess = False Then
            'Added so it gives error if validation fields failed
            SendError("Validations failed. Nothing saved.", False)
        End If
        Return bSuccess
    End Function

Open in new window

The suggestion you gave doesn't seem to work. I tied the function I have to the save button, but it causes an error with the validations.
The problem I am wanting to fix is that when I click the checkbox to reveal the textbox, the validation error "*Required" shows immediately. I don't want it to show until they have attempted to fill it out and then blanked it out, or hit the save button after having left it blank.
I thought if I used your suggestion, and tied the ValidatorEnable to the save button it might fix it. I commented out the ValidatorEnable on the checkbox and had it tied to the save button instead, but I was given a server side Validation failed error. It shouldn't be hitting the server side yet. It should return the error for the validation on the client side only, but it's triggering an auto postback and hitting the server side in addition to the client side. It should not hit the server side if the validations fail on the client side. The adjustments I made to the code is below:
$(document).ready(function functionCheckEmail() {
    //check if checkbox is checked on page load
    if ($('#chkIsEmailNotices').is(':checked')) {

        $("#" + idEmail.txtEmailNotification).show(); //shows if checked
        //$("#" + idEmailVal.valRegExpEmailNotification).show(); //doesn't work for validators
        ValidatorEnable($("#" + idEmailVal.valRegExpEmailNotification)[0], true); //shows validators
        //ValidatorEnable($("#" + idEmailReqVal.valEmailNotification)[0], true); //shows validators

    } else {

        $("#" + idEmail.txtEmailNotification).hide(); //hides if not checked
        //$("#" + idEmailVal.valRegExpEmailNotification).hide(); //doesn't work for validators
        ValidatorEnable($("#" + idEmailVal.valRegExpEmailNotification)[0], false); //hides validators
        ValidatorEnable($("#" + idEmailReqVal.valEmailNotification)[0], false); //hides validators
    }
    $('#chkIsEmailNotices').click(functionCheckEmail);
    $('#btnSaveNotifications').click(function () {
        ValidatorEnable($("#" + idEmailReqVal.valEmailNotification)[0], true);

    });
});

Open in new window


UPDATE: I needed to change the CliendIDMode to Static, so the javascript would work. I could have also used a variable, but changing it to static was easier. Anyway, his solution worked once I did that. Thanks!
Thanks Zakaria!