validation on biz form textbox

Mehrdad ilchizadeh asked on October 21, 2015 10:06

hello i want validation for my textbox in bizform like this:

<form id="form1" runat="server">
  <asp:TextBox ID="txtOne" runat="server" />
  <asp:RequiredFieldValidator ID="rfv" runat="server" 
                             ControlToValidate="txtOne" Text="SomeText 1" />
  <asp:TextBox ID="txtTwo" runat="server" />
  <asp:RequiredFieldValidator ID="rfv2" runat="server" 
                             ControlToValidate="txtTwo" Text="SomeText 2" />
  <asp:Button ID="btnOne" runat="server" OnClientClick="return BtnClick();" 
                                     Text="Click" CausesValidation="true" />
</form>
<script type="text/javascript">
    function BtnClick() {
        //var v1 = "#<%= rfv.ClientID %>";
        //var v2 = "#<%= rfv2.ClientID %>";
        var val = Page_ClientValidate();
        if (!val) {
            var i = 0;
            for (; i < Page_Validators.length; i++) {
                if (!Page_Validators[i].isvalid) {
                    $("#" + Page_Validators[i].controltovalidate)
                     .css("background-color", "red");
                }
            }
        }            
        return val;
    }
</script>

Correct Answer

Brenden Kehren answered on October 21, 2015 14:13

By default, Kentico already assigns a CSS class to the control's parent in error. See the sample markup:

<li>  
  <label class="EditingFormLabel" for="txtText">First name:</label>
  <div class="EditingFormControlNestedControl editing-form-control-nested-control Error">
    <input name="txtText" type="text" maxlength="200" class="form-control">
  </div>
  <span class="EditingFormErrorLabel">Please enter a value.</span>
</li>

So there should be no need to create a javascript function to do this. Simply add this style:div.Error input { background-color: red; }

2 votesVote for this answer Unmark Correct answer

Recent Answers


Mehrdad ilchizadeh answered on October 21, 2015 14:41

can i set this error by no focus in input... i don't want see error after submit...??

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on October 21, 2015 17:13

Right now, I don't believe this is available OOTB, so you might have to write some JS for this.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.