1

I want to do something like the following in a visualforce page

<apex:inputTextarea id="abc" 
        value="if('abc'.value().size()>0,alert('Hi'),{!AModel.Comment.Body}"

Can anyone tell me how to achieve this

I want to display an alert if the entered value is null. else I want to pass it to the controller.

9
  • why don't you just check the AModel.Comment.Body this is null or not in your controller and show error message. else when you try to submit/save the page. before calling the controller method, call a javascript function there check this field is empty or not. and based on that show alert message Commented Jun 16, 2016 at 12:06
  • That is not possible actually... The error message has to come in an alert box. Commented Jun 16, 2016 at 12:12
  • what is not possible? the controller validation or javascript alert message? Commented Jun 16, 2016 at 12:13
  • It has to be a javascript alert message. It cant be a controller validation. Commented Jun 16, 2016 at 12:14
  • 1
    Use the Ratan's code of Javascript in a Function and Call that function from the Onclick of Command Button... Commented Jun 16, 2016 at 12:28

1 Answer 1

0

You can add a javascript validation like below

<apex:inputTextarea id="abc" 
        value="{!AModel.Comment.Body}" />

in javascript

var commentBodyVal = document.getElementById('{!$Component.abc}').value;
if(commentBodyVal.trim().length == 0 )
{
    alert('Your text area field is empty. Please add something.');
}
else {
   //call action function to call your controller method
}
5
  • document.getElementById('{!$Component.abc}').value statement is throwing an error.. it it saying cannot read the .value property of null @Ratan Commented Jun 20, 2016 at 12:11
  • @Rimii may be your component is inside multiple VF tags that s why it is not able to access .. check this doc developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… Commented Jun 20, 2016 at 12:15
  • @Rimii your id may be like this {!$Component.theBlock.theSection.theSectionItem.theText} multi component ids Commented Jun 20, 2016 at 12:21
  • @Rimii is tis question still open ? Commented Jun 29, 2016 at 6:46
  • no the question is not open Commented Jun 29, 2016 at 7:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.