ASP.NET - requirfield validator not working in multiline textbox

Asked By msakt on 06-Jun-12 01:06 AM
requirfield validator not working in multiline textbox.....if remove textmode="multiline" its work

Jitendra Faye replied to msakt on 06-Jun-12 01:09 AM
Yes, If you will set textMode for TextBox = MultiNlie then RequiredFieldValidator will not work,

for this you need to explicitly write javascript code.
Jitendra Faye replied to msakt on 06-Jun-12 01:11 AM
Use this javascript function to validate textBox-

function validation()
 {

if(document.getElementById("<%=TextBox1.ClientID%>").value=="")
 {
 alert("Value can not be blank");
 document.getElementById("<%=TextBox1.ClientID%>").focus();
 return false; 
}


}
Try this and let me know.
Chintan Vaghela replied to msakt on 06-Jun-12 02:06 AM

Hi Frndz,

 

Check Validator on textbox OnBlur javascript Event

 

Made your aspx page texbox , validator and javascript as following way

 

   <asp:TextBox ID="txtMultiline" runat="server" TextMode="MultiLine" onBlur="rfvBlur()" Columns="4" ValidationGroup="Multi"></asp:TextBox>

    <asp:RequiredFieldValidator ID="rfv" runat="server" ErrorMessage="*" ControlToValidate="txtMultiline" ValidationGroup="Multi">

 

 

<script type="text/javascript">

    function rfvBlur() {

      var rfv = document.getElementById("<%= rfv.ClientID %>");

      ValidatorValidate(rfv);

    }   

</script>

 

 

Hope this helpful!

Thanks

 

 

 

 

Somesh Yadav replied to msakt on 06-Jun-12 02:42 AM

The RequiredFieldValidator is triggered by the client side onchange event. It sounds like you're expecting it to be triggered by the onblur event, such that tabbing away from the textbox would fire the validation.

Before jumping to that, I suspect this is what you are seeing and to validate that it's actually working you need to trigger onchange. To do so, enter some text in the textbox, tab away, tab back to it, clear the textbox, then tab away once more. You should now see the RequiredFieldValidator's error message since it's contents have changed.

Back to the onblur issue. To accomplish that behavior you could add the onblur attribute in your code-behind and have it call the ValidatorValidate(...) JavaScript method as follows:

void Page_Load(object sender, EventArgs e)
{
    txtSummary
.Attributes.Add("onblur", "ValidatorValidate(" + reqvalSummary.ClientID + ")");
}

Alternately, you could accomplish the same thing in markup. First, add this script block:

<script type="text/javascript">
   
function rfvBlur() {
       
var rfv = document.getElementById("<%= reqvalSummary.ClientID %>");
       
ValidatorValidate(rfv);
   
}    
</script>

Second, update the <asp:TextBox.../> markup by adding onblur="rfvBlur()" so that it now looks like this:

<asp:TextBox ID="txtSummary" runat="server" TextMode="MultiLine" Width="700px" CausesValidation="true"
           
CssClass="txtStyle" Font-Names="Arial" MaxLength="1000" ValidationGroup="Valtxt"
           
TabIndex="2" Rows="4" onblur="rfvBlur()" />

Yet another option is to validate the entire ValidationGroup by adding the following attribute to your <asp:TextBox.../> markup (no additional script block needed):

onblur="Page_ClientValidate('Valtxt')"
dipa ahuja replied to msakt on 06-Jun-12 03:09 AM
Use javascript 

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="IsEmpty();return false;" />
 
<script type="text/ecmascript">
 
function IsEmpty() {
 
  var name = document.getElementById("<%= TextBox1.ClientID %>").value;
  if (name == "") {
    alert("Name can't be empty");
    name.focus();      
  }
}
</script>